Index: cmake/modules/SingleMultiSource.cmake =================================================================== --- cmake/modules/SingleMultiSource.cmake +++ cmake/modules/SingleMultiSource.cmake @@ -2,18 +2,46 @@ # # Defines helpers to add executables and tests. The entry points to this # file are: -# `llvm_singlesource()` and +# `llvm_test_executable(name sources...)`, +# `llvm_singlesource()`, and # `llvm_multisource()` # -# Each is a macro that uses the environment it was called in to determine -# what to build and how, and generates a test file that can be given to LIT. -# The test file is generated at configure time. +# llvm_test_executable(name sources...) +# Main macro for test creation. +# name -- base name for the test target to create +# source.. -- list of files to compile. # +# Following convenience macros provide shortcuts for common test cases: +# +# llvm_singlesource() +# Invokes llvm_test_executable() for each c/c++ source file in current directory. +# +# llvm_multisource() +# Invokes llvm_test_executable(PROG ${Sources}|Glob()) +# +# Variables that control target generation: +# TARGET_PREFIX - prefix to add to base name of the test executalbe so it can +# be distinguished from other tests with the same base name. +# PROGRAMS_TO_SKIP - list of base names of executalbes to skip. +# +# Results: +# LLVM_TEST_EXECUTABLE - name of the executable target created by the last +# invocation of llvm_test_executable(). +# LLVM_SINGLESOURCE_EXECUTABLES - names of executables created by the +# last invocation of llvm_singlesource(). ##===----------------------------------------------------------------------===## include(TestFile) +# Sets Var to ${name} with directory and shortest extension removed. +macro(basename Var name) + # strip directory name + get_filename_component(_filename ${name} NAME) + # remove shortest extension. + string(REGEX REPLACE "\\.[^.]*$" "" ${Var} ${_filename}) +endmacro() + # Set unique target prefix within caller's scope. function(llvm_target_prefix prefix) if(prefix) @@ -23,16 +51,6 @@ endif() endfunction() -# Given a source file name after which a test should be named, create a unique -# name for the test. Usually this is just the source file with the suffix -# stripped, and ${TARGET_PREFIX} prepended. -function(get_unique_exe_name new_name main_src) - string(REGEX REPLACE ".[cp]+$" "" path ${main_src}) - get_filename_component(name ${path} NAME ) - - set(${new_name} "${TARGET_PREFIX}${name}" PARENT_SCOPE) -endfunction() - # Add flags to a cmake target property. macro(append_target_flags propertyname target) if(NOT "${ARGN}" STREQUAL "") @@ -116,15 +134,18 @@ llvm_add_test(${testfile} ${executable}) endfunction() -macro(test_suite_add_executable name mainsource) +macro(llvm_test_executable name) + set(LLVM_TEST_EXECUTABLE) list(FIND PROGRAMS_TO_SKIP ${name} name_idx) # Should we skip this? if(${name_idx} EQUAL -1) - get_unique_exe_name(executable ${mainsource}) + set(executable "${TARGET_PREFIX}${name}") add_executable(${executable} ${ARGN}) append_compile_flags(${executable} ${CFLAGS}) append_compile_flags(${executable} ${CPPFLAGS}) append_compile_flags(${executable} ${CXXFLAGS}) + target_include_directories(${executable} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + target_include_directories(${executable} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) # Note that we cannot use target_link_libraries() here because that one # only interprets inputs starting with '-' as flags. append_link_flags(${executable} ${LDFLAGS}) @@ -147,19 +168,19 @@ add_dependencies(${executable} timeit-target) endif() add_dependencies(${executable} timeit-host fpcmp-host) + set(LLVM_TEST_EXECUTABLE ${executable}) endif() endmacro() # Configure the current directory as a SingleSource subdirectory - i.e. every # file in *.{c,cpp,cc} is treated as its own test. macro(llvm_singlesource) + set(LLVM_SINGLESOURCE_EXECUTABLES) file(GLOB sources *.c *.cpp *.cc) foreach(source ${sources}) - # Find the pure name of the test - string(REGEX REPLACE ".[cp]+$" "" path ${source}) - string(REGEX REPLACE ".*/" "" name ${path}) - - test_suite_add_executable(${name} ${source} ${source}) + basename(name ${source}) + llvm_test_executable(${name} ${source}) + list(APPEND LLVM_SINGLESOURCE_EXECUTABLES ${LLVM_TEST_EXECUTABLE}) endforeach() endmacro() @@ -175,8 +196,7 @@ list(LENGTH sources sources_len) if(sources_len GREATER 0 AND DEFINED PROG) - include_directories(${CMAKE_CURRENT_SOURCE_DIR}) - include_directories(${CMAKE_CURRENT_BINARY_DIR}) - test_suite_add_executable(${PROG} "${PROG}.c" ${sources}) + llvm_test_executable(${PROG} ${sources}) endif() endmacro() +