Index: flang/CMakeLists.txt =================================================================== --- flang/CMakeLists.txt +++ flang/CMakeLists.txt @@ -431,6 +431,9 @@ add_subdirectory(runtime) option(FLANG_BUILD_EXAMPLES "Build Flang example programs by default." OFF) +set(FLANG_EXAMPLES_INSTALL_DIR "examples" CACHE STRING + "Path for examples subdirectory (enabled by FLANG_BUILD_EXAMPLES=ON) (defaults to 'examples')") +mark_as_advanced(FLANG_EXAMPLES_INSTALL_DIR) if (FLANG_BUILD_EXAMPLES AND FLANG_STANDALONE_BUILD) message(FATAL_ERROR "Examples are not supported in out-of-tree builds.") endif() Index: flang/cmake/modules/AddFlang.cmake =================================================================== --- flang/cmake/modules/AddFlang.cmake +++ flang/cmake/modules/AddFlang.cmake @@ -128,3 +128,24 @@ llvm_install_symlink(FLANG ${name} ${dest} ALWAYS_GENERATE) endmacro() +macro(add_flang_example name) + if (NOT FLANG_BUILD_EXAMPLES) + set(EXCLUDE_FROM_ALL ON) + endif() + add_llvm_executable(${name} ${ARGN}) + if (FLANG_BUILD_EXAMPLES) + install(TARGETS ${name} RUNTIME DESTINATION "${FLANG_EXAMPLES_INSTALL_DIR}") + endif() + set_target_properties(${name} PROPERTIES FOLDER "Examples") +endmacro(add_flang_example name) + +macro(add_flang_example_library name) + if (NOT FLANG_BUILD_EXAMPLES) + set(EXCLUDE_FROM_ALL ON) + add_llvm_library(${name} BUILDTREE_ONLY ${ARGN}) + else() + add_llvm_library(${name} ${ARGN}) + endif() + + set_target_properties(${name} PROPERTIES FOLDER "Examples") +endmacro(add_flang_example_library name) Index: flang/examples/CMakeLists.txt =================================================================== --- flang/examples/CMakeLists.txt +++ flang/examples/CMakeLists.txt @@ -1,7 +1,3 @@ -if(NOT FLANG_BUILD_EXAMPLES) - set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL ON) -endif() - add_subdirectory(ExternalHelloWorld) add_subdirectory(PrintFlangFunctionNames) add_subdirectory(FlangOmpReport) Index: flang/examples/ExternalHelloWorld/CMakeLists.txt =================================================================== --- flang/examples/ExternalHelloWorld/CMakeLists.txt +++ flang/examples/ExternalHelloWorld/CMakeLists.txt @@ -1,8 +1,8 @@ +set(LLVM_LINK_COMPONENTS + FortranRuntime + ) + # This test is not run by default as it requires input. -add_executable(external-hello-world +add_flang_example(external-hello-world external-hello.cpp ) - -target_link_libraries(external-hello-world - FortranRuntime -) Index: flang/examples/FlangOmpReport/CMakeLists.txt =================================================================== --- flang/examples/FlangOmpReport/CMakeLists.txt +++ flang/examples/FlangOmpReport/CMakeLists.txt @@ -1,4 +1,4 @@ -add_llvm_library(flangOmpReport +add_flang_example_library(flangOmpReport MODULE FlangOmpReport.cpp FlangOmpReportVisitor.cpp Index: flang/examples/PrintFlangFunctionNames/CMakeLists.txt =================================================================== --- flang/examples/PrintFlangFunctionNames/CMakeLists.txt +++ flang/examples/PrintFlangFunctionNames/CMakeLists.txt @@ -1,6 +1,6 @@ # TODO: Note that this is currently only available on Linux. # On Windows, we would also have to specify e.g. `PLUGIN_TOOL`. -add_llvm_library(flangPrintFunctionNames +add_flang_example_library(flangPrintFunctionNames MODULE PrintFlangFunctionNames.cpp