1 function(_add_files_tgt tgt)
2 cmake_parse_arguments(PARAM "" "" "CONDITION" ${ARGN})
3 set(PARAM_FILES "${PARAM_UNPARSED_ARGUMENTS}")
6 if(NOT (${PARAM_CONDITION}))
11 foreach(FILE IN LISTS PARAM_FILES)
12 # Some IDEs are not happy with duplicated filenames, so we detect that before adding the file.
13 get_target_property(${tgt}_FILES ${tgt} SOURCES)
14 if(${tgt}_FILES MATCHES "/${FILE}(;|$)")
15 string(REGEX REPLACE "(^|.+;)([^;]+/${FILE})(;.+|$)" "\\2" RES "${${tgt}_FILES}")
16 # Ignore header files duplicates in 3rdparty.
17 if(NOT (${FILE} MATCHES "\.h" AND (${RES} MATCHES "3rdparty" OR ${CMAKE_CURRENT_SOURCE_DIR} MATCHES "3rdparty")))
18 message(FATAL_ERROR "${tgt}: ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} filename is a duplicate of ${RES}")
22 target_sources(${tgt} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
26 # Add a file to be compiled.
28 # add_files([file1 ...] CONDITION condition [condition ...])
30 # CONDITION is a complete statement that can be evaluated with if().
31 # If it evaluates true, the source files will be added; otherwise not.
32 # For example: ADD_IF SDL_FOUND AND Allegro_FOUND
35 _add_files_tgt(openttd_lib ${ARGV})
38 # Add a test file to be compiled.
40 # add_test_files([file1 ...] CONDITION condition [condition ...])
42 # CONDITION is a complete statement that can be evaluated with if().
43 # If it evaluates true, the source files will be added; otherwise not.
44 # For example: ADD_IF SDL_FOUND AND Allegro_FOUND
46 function(add_test_files)
47 _add_files_tgt(openttd_test ${ARGV})
50 # This function works around an 'issue' with CMake, where
51 # set_source_files_properties() only works in the scope of the file. We want
52 # to set properties for the source file on a more global level. To solve this,
53 # this function records the flags you want, and a macro adds them in the root
55 # See this URL for more information on the issue:
56 # http://cmake.3232098.n2.nabble.com/scope-of-set-source-files-properties-td4766111.html
58 # set_compile_flags([file1 ...] COMPILE_FLAGS cflag [cflag ...])
60 function(set_compile_flags)
61 cmake_parse_arguments(PARAM "" "" "COMPILE_FLAGS" ${ARGN})
62 set(PARAM_FILES "${PARAM_UNPARSED_ARGUMENTS}")
64 get_property(SOURCE_PROPERTIES GLOBAL PROPERTY source_properties)
66 foreach(FILE IN LISTS PARAM_FILES)
67 list(APPEND SOURCE_PROPERTIES "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}::${PARAM_COMPILE_FLAGS}")
70 set_property(GLOBAL PROPERTY source_properties "${SOURCE_PROPERTIES}")
73 # Call this macro in the same CMakeLists.txt and after add_executable().
74 # This makes sure all the COMPILE_FLAGS of set_compile_flags() are set
77 # process_compile_flags()
79 function(process_compile_flags)
80 get_property(SOURCE_PROPERTIES GLOBAL PROPERTY source_properties)
82 foreach(ENTRY ${SOURCE_PROPERTIES})
83 string(REPLACE "::" ";" ENTRY "${ENTRY}")
84 list(GET ENTRY 0 FILE)
85 list(GET ENTRY 1 PROPERTIES)
87 set_source_files_properties(${FILE} PROPERTIES COMPILE_FLAGS ${PROPERTIES})