From 6c9326f76047f9a172820df2549ac592eb34b0eb Mon Sep 17 00:00:00 2001
From: Derek Bankieris <derek.r.bankieris@nasa.gov>
Date: Wed, 19 Jun 2019 15:44:45 -0500
Subject: [PATCH] Add makefile function for target-specific variable

Closes #672
---
 libexec/trick/make_makefile_src                  |  5 +++++
 test/SIM_target_specific_variables/S_define      | 11 +++++++++++
 .../SIM_target_specific_variables/S_overrides.mk | 16 ++++++++++++++++
 .../models/many/a.cpp                            |  1 +
 .../models/many/a.hh                             |  1 +
 .../models/many/b.cpp                            |  1 +
 .../models/many/b.hh                             |  1 +
 .../models/many/c.cpp                            |  1 +
 .../models/many/c.hh                             |  1 +
 .../models/nested/x.cpp                          |  1 +
 .../models/nested/x.hh                           |  1 +
 .../models/nested/y/y.cpp                        |  1 +
 .../models/nested/y/y.hh                         |  1 +
 .../models/nested/z/z.cpp                        |  1 +
 .../models/nested/z/z.hh                         |  1 +
 .../models/one/1.cpp                             |  1 +
 .../models/one/1.hh                              |  1 +
 test/makefile                                    |  1 +
 18 files changed, 47 insertions(+)
 create mode 100644 test/SIM_target_specific_variables/S_define
 create mode 100644 test/SIM_target_specific_variables/S_overrides.mk
 create mode 100644 test/SIM_target_specific_variables/models/many/a.cpp
 create mode 100644 test/SIM_target_specific_variables/models/many/a.hh
 create mode 100644 test/SIM_target_specific_variables/models/many/b.cpp
 create mode 100644 test/SIM_target_specific_variables/models/many/b.hh
 create mode 100644 test/SIM_target_specific_variables/models/many/c.cpp
 create mode 100644 test/SIM_target_specific_variables/models/many/c.hh
 create mode 100644 test/SIM_target_specific_variables/models/nested/x.cpp
 create mode 100644 test/SIM_target_specific_variables/models/nested/x.hh
 create mode 100644 test/SIM_target_specific_variables/models/nested/y/y.cpp
 create mode 100644 test/SIM_target_specific_variables/models/nested/y/y.hh
 create mode 100644 test/SIM_target_specific_variables/models/nested/z/z.cpp
 create mode 100644 test/SIM_target_specific_variables/models/nested/z/z.hh
 create mode 100644 test/SIM_target_specific_variables/models/one/1.cpp
 create mode 100644 test/SIM_target_specific_variables/models/one/1.hh

diff --git a/libexec/trick/make_makefile_src b/libexec/trick/make_makefile_src
index 8002abb1..5f16a0a2 100755
--- a/libexec/trick/make_makefile_src
+++ b/libexec/trick/make_makefile_src
@@ -245,6 +245,11 @@ build/S_source.d: ;
 
 # MODEL_OBJECTS ================================================================
 
+# This function allows users to specify target-specific variables for individual
+# or groups of source files. For example:
+# \$(call FIND_MODEL_OBJECTS, /path/to/file /path/to/directory \$(LIST_OF_PATHS)): TRICK_CXXFLAGS += -Werror
+FIND_MODEL_OBJECTS = \$(foreach PATH, \$1, \$(filter \$(addprefix build, \$(addsuffix %, \$(PATH))), \$(MODEL_OBJECTS)))
+
 " ;
 
 # List out all of the object files and put the list in a file that we can pass to the linker.
diff --git a/test/SIM_target_specific_variables/S_define b/test/SIM_target_specific_variables/S_define
new file mode 100644
index 00000000..7cf3d2a1
--- /dev/null
+++ b/test/SIM_target_specific_variables/S_define
@@ -0,0 +1,11 @@
+#include "sim_objects/default_trick_sys.sm"
+
+##include "one/1.hh"
+##include "many/a.hh"
+##include "many/b.hh"
+##include "many/c.hh"
+##include "nested/x.hh"
+##include "nested/y/y.hh"
+##include "nested/z/z.hh"
+
+class Sandbox : public Trick::SimObject {};
diff --git a/test/SIM_target_specific_variables/S_overrides.mk b/test/SIM_target_specific_variables/S_overrides.mk
new file mode 100644
index 00000000..11dd3924
--- /dev/null
+++ b/test/SIM_target_specific_variables/S_overrides.mk
@@ -0,0 +1,16 @@
+MODELS :=$(CURDIR)/models
+
+# Warn about comment markers /* within comments and make them errors, causing
+# the build to fail.
+TRICK_CXXFLAGS += -I$(MODELS) -Wcomment -Werror
+
+PATHS := $(MODELS)/many $(MODELS)/nested
+
+# The zero directory contains no files, so FIND_MODEL_OBJECTS should return an
+# empty list, resulting in a rule that is never run. If it is, the invalid
+# "not_a_flag" should cause the build to fail.
+$(call FIND_MODEL_OBJECTS, $(MODELS)/zero): TRICK_CXXFLAGS += not_a_flag
+
+# By turning off warnings about comment markers within comments in all the
+# offending files, this sim should build without error.
+$(call FIND_MODEL_OBJECTS, $(MODELS)/one/1 $(PATHS)): TRICK_CXXFLAGS += -Wno-comment
diff --git a/test/SIM_target_specific_variables/models/many/a.cpp b/test/SIM_target_specific_variables/models/many/a.cpp
new file mode 100644
index 00000000..b23abf61
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/many/a.cpp
@@ -0,0 +1 @@
+/* /* */
diff --git a/test/SIM_target_specific_variables/models/many/a.hh b/test/SIM_target_specific_variables/models/many/a.hh
new file mode 100644
index 00000000..aa099577
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/many/a.hh
@@ -0,0 +1 @@
+// @trick_link_dependency{many/a.cpp}
diff --git a/test/SIM_target_specific_variables/models/many/b.cpp b/test/SIM_target_specific_variables/models/many/b.cpp
new file mode 100644
index 00000000..b23abf61
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/many/b.cpp
@@ -0,0 +1 @@
+/* /* */
diff --git a/test/SIM_target_specific_variables/models/many/b.hh b/test/SIM_target_specific_variables/models/many/b.hh
new file mode 100644
index 00000000..b1381a3f
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/many/b.hh
@@ -0,0 +1 @@
+// @trick_link_dependency{many/b.cpp}
diff --git a/test/SIM_target_specific_variables/models/many/c.cpp b/test/SIM_target_specific_variables/models/many/c.cpp
new file mode 100644
index 00000000..b23abf61
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/many/c.cpp
@@ -0,0 +1 @@
+/* /* */
diff --git a/test/SIM_target_specific_variables/models/many/c.hh b/test/SIM_target_specific_variables/models/many/c.hh
new file mode 100644
index 00000000..07cabbcf
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/many/c.hh
@@ -0,0 +1 @@
+// @trick_link_dependency{many/c.cpp}
diff --git a/test/SIM_target_specific_variables/models/nested/x.cpp b/test/SIM_target_specific_variables/models/nested/x.cpp
new file mode 100644
index 00000000..b23abf61
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/nested/x.cpp
@@ -0,0 +1 @@
+/* /* */
diff --git a/test/SIM_target_specific_variables/models/nested/x.hh b/test/SIM_target_specific_variables/models/nested/x.hh
new file mode 100644
index 00000000..5e26d0ed
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/nested/x.hh
@@ -0,0 +1 @@
+// @trick_link_dependency{nested/x.cpp}
diff --git a/test/SIM_target_specific_variables/models/nested/y/y.cpp b/test/SIM_target_specific_variables/models/nested/y/y.cpp
new file mode 100644
index 00000000..b23abf61
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/nested/y/y.cpp
@@ -0,0 +1 @@
+/* /* */
diff --git a/test/SIM_target_specific_variables/models/nested/y/y.hh b/test/SIM_target_specific_variables/models/nested/y/y.hh
new file mode 100644
index 00000000..1202525c
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/nested/y/y.hh
@@ -0,0 +1 @@
+// @trick_link_dependency{nested/y/y.cpp}
diff --git a/test/SIM_target_specific_variables/models/nested/z/z.cpp b/test/SIM_target_specific_variables/models/nested/z/z.cpp
new file mode 100644
index 00000000..b23abf61
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/nested/z/z.cpp
@@ -0,0 +1 @@
+/* /* */
diff --git a/test/SIM_target_specific_variables/models/nested/z/z.hh b/test/SIM_target_specific_variables/models/nested/z/z.hh
new file mode 100644
index 00000000..f60d99f1
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/nested/z/z.hh
@@ -0,0 +1 @@
+// @trick_link_dependency{nested/z/z.cpp}
diff --git a/test/SIM_target_specific_variables/models/one/1.cpp b/test/SIM_target_specific_variables/models/one/1.cpp
new file mode 100644
index 00000000..b23abf61
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/one/1.cpp
@@ -0,0 +1 @@
+/* /* */
diff --git a/test/SIM_target_specific_variables/models/one/1.hh b/test/SIM_target_specific_variables/models/one/1.hh
new file mode 100644
index 00000000..ac88dbd9
--- /dev/null
+++ b/test/SIM_target_specific_variables/models/one/1.hh
@@ -0,0 +1 @@
+// @trick_link_dependency{one/1.cpp}
diff --git a/test/makefile b/test/makefile
index d4ef47ba..2936432b 100644
--- a/test/makefile
+++ b/test/makefile
@@ -8,6 +8,7 @@ COMPILE_DIRS = \
 	SIM_python_namespace \
 	SIM_rti \
 	SIM_stls \
+	SIM_target_specific_variables \
 	SIM_test_dp \
 	SIM_test_dr \
 	SIM_test_io \