55# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
77
8- function (_add_sharkfuser_target)
8+ # Creates an executable target for use in a test.
9+ #
10+ # NAME
11+ # The name of the executable target to create (required)
12+ #
13+ # SRCS
14+ # Source files to compile into the executable (required)
15+ #
16+ # DEPS
17+ # Additional library dependencies beyond the standard ones
18+ # (libfusili and Catch2::Catch2WithMain are always linked)
19+ #
20+ # BIN_SUBDIR
21+ # Subdirectory under build/bin/ where the executable will be placed
22+ function (_add_sharkfuser_executable_for_test)
923 cmake_parse_arguments (
1024 _RULE # prefix
1125 "" # options
@@ -29,6 +43,33 @@ function(_add_sharkfuser_target)
2943 target_link_options (${_RULE_NAME} PRIVATE -coverage)
3044 endif ()
3145
46+ # Place executable in the build/bin sub-directory
47+ set_target_properties (
48+ ${_RULE_NAME} PROPERTIES
49+ RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /bin/${_RULE_BIN_SUBDIR}
50+ )
51+ endfunction ()
52+
53+
54+ # Creates an executable target + test.
55+ function (_add_sharkfuser_ctest_target)
56+ cmake_parse_arguments (
57+ _RULE # prefix
58+ "" # options
59+ "NAME;BIN_SUBDIR" # one value keywords
60+ "SRCS;DEPS" # multi-value keywords
61+ ${ARGN} # extra arguments
62+ )
63+
64+ # Create the target first
65+ _add_sharkfuser_executable_for_test(
66+ NAME ${_RULE_NAME}
67+ SRCS ${_RULE_SRCS}
68+ DEPS ${_RULE_DEPS}
69+ BIN_SUBDIR ${_RULE_BIN_SUBDIR}
70+ )
71+
72+ # Add the test
3273 add_test (NAME ${_RULE_NAME} COMMAND ${_RULE_NAME} )
3374
3475 # Set logging environment variables
@@ -38,16 +79,25 @@ function(_add_sharkfuser_target)
3879 ENVIRONMENT "FUSILI_LOG_INFO=1;FUSILI_LOG_FILE=stdout"
3980 )
4081 endif ()
41-
42- # Place executable in the build/bin sub-directory
43- set_target_properties (
44- ${_RULE_NAME} PROPERTIES
45- RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /bin/${_RULE_BIN_SUBDIR}
46- )
4782endfunction ()
4883
4984
85+ # Creates a sharkfuser test.
86+ #
87+ # NAME
88+ # The name of the executable target to create (required)
89+ #
90+ # SRCS
91+ # Source files to compile into the executable (required)
92+ #
93+ # DEPS
94+ # Additional library dependencies beyond the standard ones
95+ # (libfusili and Catch2::Catch2WithMain are always linked)
5096function (add_sharkfuser_test)
97+ if (NOT SHARKFUSER_BUILD_TESTS)
98+ return ()
99+ endif ()
100+
51101 cmake_parse_arguments (
52102 _RULE
53103 ""
@@ -56,11 +106,7 @@ function(add_sharkfuser_test)
56106 ${ARGN}
57107 )
58108
59- if (NOT SHARKFUSER_BUILD_TESTS)
60- return ()
61- endif ()
62-
63- _add_sharkfuser_target(
109+ _add_sharkfuser_ctest_target(
64110 NAME ${_RULE_NAME}
65111 SRCS ${_RULE_SRCS}
66112 DEPS ${_RULE_DEPS}
@@ -69,7 +115,22 @@ function(add_sharkfuser_test)
69115endfunction ()
70116
71117
118+ # Creates a sharkfuser sample.
119+ #
120+ # NAME
121+ # The name of the executable target to create (required)
122+ #
123+ # SRCS
124+ # Source files to compile into the executable (required)
125+ #
126+ # DEPS
127+ # Additional library dependencies beyond the standard ones
128+ # (libfusili and Catch2::Catch2WithMain are always linked)
72129function (add_sharkfuser_sample)
130+ if (NOT SHARKFUSER_BUILD_SAMPLES)
131+ return ()
132+ endif ()
133+
73134 cmake_parse_arguments (
74135 _RULE
75136 ""
@@ -78,14 +139,87 @@ function(add_sharkfuser_sample)
78139 ${ARGN}
79140 )
80141
81- if (NOT SHARKFUSER_BUILD_SAMPLES)
82- return ()
83- endif ()
84-
85- _add_sharkfuser_target(
142+ _add_sharkfuser_ctest_target(
86143 NAME ${_RULE_NAME}
87144 SRCS ${_RULE_SRCS}
88145 DEPS ${_RULE_DEPS}
89146 BIN_SUBDIR samples
90147 )
91148endfunction ()
149+
150+
151+ # Creates a lit test that compiles a source file and runs lit on it.
152+ #
153+ # add_sharkfuser_lit_test(
154+ # SRC <source-file>
155+ # [TOOLS <tool1> <tool2> ...]
156+ # [DEPS <dep1> <dep2> ...]
157+ # )
158+ #
159+ # SRC
160+ # The source file to compile and test (required)
161+ #
162+ # TOOLS
163+ # External tools needed for the test (e.g., FileCheck)
164+ #
165+ # DEPS
166+ # Library dependencies for the compiled executable
167+ function (add_sharkfuser_lit_test)
168+ if (NOT SHARKFUSER_BUILD_TESTS)
169+ return ()
170+ endif ()
171+
172+ cmake_parse_arguments (
173+ _RULE # prefix
174+ "" # options
175+ "SRC" # one value keywords
176+ "TOOLS;DEPS" # multi-value keywords
177+ ${ARGN}
178+ )
179+ if (NOT _RULE_SRC)
180+ message (FATAL_ERROR "add_sharkfuser_lit_test: SRC parameter is required" )
181+ endif ()
182+
183+ get_filename_component (_ABSOLUTE_RULE_SRC ${_RULE_SRC} ABSOLUTE )
184+ get_filename_component (_TEST_NAME ${_RULE_SRC} NAME_WE )
185+
186+ # The executable who's output is being lit tested.
187+ _add_sharkfuser_executable_for_test(
188+ NAME ${_TEST_NAME}
189+ SRCS ${_RULE_SRC}
190+ DEPS ${_RULE_DEPS}
191+ BIN_SUBDIR lit
192+ )
193+
194+ # Pass lit locations of tools in build directory through `--path` arguments.
195+ set (_LIT_PATH_ARGS
196+ "--path" "$<TARGET_FILE_DIR:${_TEST_NAME} >" # include test itself
197+ "--path" "$<TARGET_FILE_DIR:FileCheck>" # include FileCheck by default
198+ )
199+ foreach (_TOOL IN LISTS _RULE_TOOLS)
200+ list (APPEND _LIT_PATH_ARGS "--path" "$<TARGET_FILE_DIR:${_TOOL} >" )
201+ endforeach ()
202+
203+ add_test (
204+ NAME
205+ ${_TEST_NAME}
206+ COMMAND
207+ "${Python3_EXECUTABLE} "
208+ "${LLVM_EXTERNAL_LIT} "
209+ "${_ABSOLUTE_RULE_SRC} "
210+ ${_LIT_PATH_ARGS}
211+ # lit config sets the "%test_exe" substitution based on this param.
212+ "--param" "TEST_EXE=$<TARGET_FILE:${_TEST_NAME} >"
213+ # Ensures lit prints a (more) useful error message on failure.
214+ "--verbose"
215+ )
216+
217+ # Apparently this flag helps FileCheck spit out nicer error messages.
218+ set_tests_properties (${_TEST_NAME} PROPERTIES ENVIRONMENT
219+ "FILECHECK_OPTS=--enable-var-scope" )
220+
221+ # Dependencies for the test.
222+ if (_RULE_TOOLS)
223+ set_tests_properties (${_TEST_NAME} PROPERTIES DEPENDS "${_RULE_TOOLS} " )
224+ endif ()
225+ endfunction ()
0 commit comments