diff --git a/cmake/GtsamTesting.cmake b/cmake/GtsamTesting.cmake index aee3ac88b..b3209e104 100644 --- a/cmake/GtsamTesting.cmake +++ b/cmake/GtsamTesting.cmake @@ -121,6 +121,69 @@ macro(gtsamAddTestsGlob groupName globPatterns excludedFiles linkLibraries) endif() endmacro() +# Macro for adding scripts - one executable per cpp file. +# globPatterns: e.g. "*.cpp", or a list of globs and files, e.g. "A*.cpp;B*.cpp". +# excludedFiles: list of files or globs to exclude, e.g. "testC*.cpp;testBroken.cpp". +# linkLibraries: list of libraries to link to. +# Usage example: gtsamAddScriptsGlob("*.cpp" "Broken.cpp" "gtsam;GeographicLib") +macro(gtsamAddExamplesGlob globPatterns excludedFiles linkLibraries) + # Get all script files + file(GLOB script_files ${globPatterns}) + + # Remove excluded scripts from the list + if(NOT "${excludedFiles}" STREQUAL "") + file(GLOB excludedFilePaths ${excludedFiles}) + if("${excludedFilePaths}" STREQUAL "") + message(WARNING "The script exclusion pattern '${excludedFiles}' did not match any files") + else() + list(REMOVE_ITEM script_files ${excludedFilePaths}) + endif() + endif() + + # Separate into source files and headers (allows for adding headers to show up in + # MSVC and Xcode projects). + set(script_srcs "") + set(script_headers "") + foreach(script_file IN ITEMS ${script_files}) + get_filename_component(script_ext ${script_file} EXT) + if(script_ext MATCHES "(h|H)") + list(APPEND script_headers ${script_file}) + else() + list(APPEND script_srcs ${script_file}) + endif() + endforeach() + + # Don't put test files in folders in MSVC and Xcode because they're already grouped + source_group("" FILES ${script_srcs} ${script_headers}) + + # Create executables + foreach(script_src IN ITEMS ${script_srcs}) + # Get script base name + get_filename_component(script_name ${script_src} NAME_WE) + + # Add executable + add_executable(${script_name} ${script_src} ${script_headers}) + target_link_libraries(${script_name} ${linkLibraries}) + + # Add target dependencies + add_dependencies(examples ${script_name}) + if(NOT MSVC AND NOT XCODE_VERSION) + add_custom_target(${script_name}.run ${EXECUTABLE_OUTPUT_PATH}${script_name}) + endif() + + # Add TOPSRCDIR + set_property(SOURCE ${script_src} APPEND PROPERTY COMPILE_DEFINITIONS "TOPSRCDIR=\"${PROJECT_SOURCE_DIR}\"") + + if(NOT GTSAM_BUILD_EXAMPLES_ALWAYS) + # Exclude from 'make all' and 'make install' + set_target_properties(${target_name} PROPERTIES EXCLUDE_FROM_ALL ON) + endif() + + # Configure target folder (for MSVC and Xcode) + set_property(TARGET ${script_name} PROPERTY FOLDER "Examples") + endforeach() +endmacro() + # Macro for adding categorized tests in a "tests" folder, with # optional exclusion of tests and convenience library linking options # diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d87070330..7251c2b6f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,42 +1,8 @@ -if(NOT MSVC) - add_custom_target(examples) -endif() - -# Build example executables -FILE(GLOB example_srcs "*.cpp") - -set (excluded_examples #"") - "${CMAKE_CURRENT_SOURCE_DIR}/DiscreteBayesNet_FG.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/UGM_chain.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/UGM_small.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/elaboratePoint2KalmanFilter.cpp" +set (excluded_examples + DiscreteBayesNet_FG.cpp + UGM_chain.cpp + UGM_small.cpp + elaboratePoint2KalmanFilter.cpp ) -list(REMOVE_ITEM example_srcs ${excluded_examples}) - -foreach(example_src ${example_srcs} ) - get_filename_component(example_base ${example_src} NAME_WE) - set( example_bin ${example_base} ) - message(STATUS "Adding Example ${example_bin}") - if(NOT MSVC) - add_dependencies(examples ${example_bin}) - endif() - add_executable(${example_bin} ${example_src}) - - # Disable building during make all/install - if (GTSAM_DISABLE_EXAMPLES_ON_INSTALL) - set_target_properties(${example_bin} PROPERTIES EXCLUDE_FROM_ALL ON) - endif() - - target_link_libraries(${example_bin} gtsam ${Boost_PROGRAM_OPTIONS_LIBRARY}) - if(NOT MSVC AND NOT XCODE_VERSION) - add_custom_target(${example_bin}.run ${EXECUTABLE_OUTPUT_PATH}${example_bin} ${ARGN}) - endif() - - # Set up Visual Studio folder - if(MSVC) - set_property(TARGET ${example_bin} PROPERTY FOLDER "Examples") - endif() - -endforeach(example_src) - +gtsamAddExamplesGlob("*.cpp" "${excluded_examples}" "gtsam;${Boost_PROGRAM_OPTIONS_LIBRARY}") diff --git a/gtsam_unstable/discrete/CMakeLists.txt b/gtsam_unstable/discrete/CMakeLists.txt index 1072999b2..18346a45a 100644 --- a/gtsam_unstable/discrete/CMakeLists.txt +++ b/gtsam_unstable/discrete/CMakeLists.txt @@ -5,27 +5,5 @@ install(FILES ${discrete_headers} DESTINATION include/gtsam_unstable/discrete) # Add all tests add_subdirectory(tests) -# List examples to build - comment out here to exclude from compilation -set(discrete_unstable_examples -schedulingExample -schedulingQuals12 -schedulingQuals13 -) - -if (GTSAM_BUILD_EXAMPLES) - foreach(example ${discrete_unstable_examples}) - add_executable(${example} "examples/${example}.cpp") - - # Disable building during make all/install - if (GTSAM_ENABLE_INSTALL_EXAMPLE_FIX) - set_target_properties(${example} PROPERTIES EXCLUDE_FROM_ALL ON) - endif() - - if(NOT MSVC AND NOT XCODE_VERSION) - add_dependencies(examples ${example}) - add_custom_target(${example}.run ${EXECUTABLE_OUTPUT_PATH}${example} ${ARGN}) - endif() - - target_link_libraries(${example} gtsam gtsam_unstable) - endforeach(example) -endif (GTSAM_BUILD_EXAMPLES) +# Add examples +add_subdirectory(examples) diff --git a/gtsam_unstable/discrete/examples/CMakeLists.txt b/gtsam_unstable/discrete/examples/CMakeLists.txt new file mode 100644 index 000000000..da06b7dfc --- /dev/null +++ b/gtsam_unstable/discrete/examples/CMakeLists.txt @@ -0,0 +1,5 @@ +set (excluded_examples + # fileToExclude.cpp +) + +gtsamAddExamplesGlob("*.cpp" "${excluded_examples}" "gtsam_unstable") diff --git a/gtsam_unstable/examples/CMakeLists.txt b/gtsam_unstable/examples/CMakeLists.txt index d95ab7706..da06b7dfc 100644 --- a/gtsam_unstable/examples/CMakeLists.txt +++ b/gtsam_unstable/examples/CMakeLists.txt @@ -1,32 +1,5 @@ -if(NOT MSVC) - add_custom_target(unstable_examples) -endif() - -# Build example executables -FILE(GLOB example_srcs "*.cpp") -foreach(example_src ${example_srcs} ) - get_filename_component(example_base ${example_src} NAME_WE) - set( example_bin ${example_base} ) - message(STATUS "Adding Example ${example_bin}") - if(NOT MSVC) - add_dependencies(examples ${example_bin}) - endif() - add_executable(${example_bin} ${example_src}) - - # Disable building during make all/install - if (GTSAM_DISABLE_EXAMPLES_ON_INSTALL) - set_target_properties(${example_bin} PROPERTIES EXCLUDE_FROM_ALL ON) - endif() - - target_link_libraries(${example_bin} gtsam gtsam_unstable) - if(NOT MSVC AND NOT XCODE_VERSION) - add_custom_target(${example_bin}.run ${EXECUTABLE_OUTPUT_PATH}${example_bin} ${ARGN}) - endif() - - # Set up Visual Studio folder - if(MSVC) - set_property(TARGET ${example_bin} PROPERTY FOLDER "Examples") - endif() - -endforeach(example_src) +set (excluded_examples + # fileToExclude.cpp +) +gtsamAddExamplesGlob("*.cpp" "${excluded_examples}" "gtsam_unstable")