1 cmake_minimum_required(VERSION 3.13.4)
3 set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
5 # Flang requires C++17.
6 set(CMAKE_CXX_STANDARD 17)
7 set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
8 set(CMAKE_CXX_EXTENSIONS OFF)
10 set(FLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
12 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE)
13 message(FATAL_ERROR "In-source builds are not allowed. \
14 Please create a directory and run cmake from there,\
15 passing the path to this source directory as the last argument.\
16 This process created the file `CMakeCache.txt' and the directory\
17 `CMakeFiles'. Please delete them.")
20 option(FLANG_ENABLE_WERROR "Fail and stop building flang if a warning is triggered." OFF)
22 # Check for a standalone build and configure as appropriate from
24 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
25 message("Building Flang as a standalone project.")
27 set(FLANG_STANDALONE_BUILD ON)
29 set(FLANG_STANDALONE_BUILD OFF)
32 # Must go below project(..)
33 include(GNUInstallDirs)
35 if (FLANG_STANDALONE_BUILD)
36 set(FLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
38 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
39 CACHE BOOL "Enable assertions")
40 # Assertions follow llvm's configuration.
41 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
44 # We need a pre-built/installed version of LLVM.
45 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
46 # If the user specifies a relative path to LLVM_DIR, the calls to include
47 # LLVM modules fail. Append the absolute path to LLVM_DIR instead.
48 get_filename_component(LLVM_DIR_ABSOLUTE ${LLVM_DIR} REALPATH)
49 list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR_ABSOLUTE})
51 # Users might specify a path to CLANG_DIR that's:
53 # * a path relative to the path of this script.
54 # Append the absolute path to CLANG_DIR so that find_package works in both
56 get_filename_component(
60 ${CMAKE_CURRENT_SOURCE_DIR})
61 list(APPEND CMAKE_MODULE_PATH ${CLANG_DIR_ABSOLUTE})
63 # TODO: Remove when libclangDriver is lifted out of Clang
64 find_package(Clang REQUIRED PATHS "${CLANG_DIR_ABSOLUTE}" NO_DEFAULT_PATH)
66 message(FATAL_ERROR "Failed to find Clang")
69 # If LLVM links to zlib we need the imported targets so we can too.
71 find_package(ZLIB REQUIRED)
73 option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
74 if(CMAKE_COMPILER_IS_GNUCXX)
75 set(USE_NO_MAYBE_UNINITIALIZED 1)
78 include(CMakeParseArguments)
80 include(HandleLLVMOptions)
81 include(VersionFromVCS)
82 include(GetErrcMessages)
87 find_package(MLIR REQUIRED CONFIG)
88 # Use SYSTEM for the same reasons as for LLVM includes
89 include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
90 # If the user specifies a relative path to MLIR_DIR, the calls to include
91 # MLIR modules fail. Append the absolute path to MLIR_DIR instead.
92 get_filename_component(MLIR_DIR_ABSOLUTE ${MLIR_DIR} REALPATH)
93 list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR_ABSOLUTE})
95 find_program(MLIR_TABLEGEN_EXE "mlir-tblgen" ${LLVM_TOOLS_BINARY_DIR}
98 option(LLVM_INSTALL_TOOLCHAIN_ONLY
99 "Only include toolchain files in the 'install' target." OFF)
100 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
101 "Set to ON to force using an old, unsupported host toolchain." OFF)
104 # Add LLVM include files as if they were SYSTEM because there are complex unused
105 # parameter issues that may or may not appear depending on the environments and
106 # compilers (ifdefs are involved). This allows warnings from LLVM headers to be
107 # ignored while keeping -Wunused-parameter a fatal error inside f18 code base.
108 # This may have to be fine-tuned if flang headers are consider part of this
109 # LLVM_INCLUDE_DIRS when merging in the monorepo (Warning from flang headers
110 # should not be suppressed).
111 include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
112 add_definitions(${LLVM_DEFINITIONS})
114 # LLVM's cmake configuration files currently sneak in a c++11 flag.
115 # We look for it here and remove it from Flang's compile flags to
116 # avoid some mixed compilation flangs (e.g. -std=c++11 ... -std=c++17).
117 if (DEFINED LLVM_CXX_STD)
118 message("LLVM configuration set a C++ standard: ${LLVM_CXX_STD}")
119 if (NOT LLVM_CXX_STD EQUAL "c++17")
120 message("Flang: Overriding LLVM's 'cxx_std' setting...")
121 message(" removing '-std=${LLVM_CXX_STD}'")
122 message(" CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'")
123 string(REPLACE " -std=${LLVM_CXX_STD}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
124 message(" [NEW] CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'")
128 link_directories("${LLVM_LIBRARY_DIR}")
130 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
131 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
132 ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
133 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
134 ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
136 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
137 set(LLVM_EXTERNAL_LIT "${LLVM_TOOLS_BINARY_DIR}/llvm-lit" CACHE STRING "Command used to spawn lit")
139 option(FLANG_INCLUDE_TESTS
140 "Generate build targets for the Flang unit tests."
143 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
145 #Handle unittests when out-of-tree
146 #LLVM_BUILD_MAIN_SRC_DIR - Path to llvm source when out-of-tree.
147 set(FLANG_GTEST_AVAIL 0)
148 if (FLANG_INCLUDE_TESTS)
149 set(UNITTEST_DIR ${LLVM_BUILD_MAIN_SRC_DIR}/utils/unittest)
150 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
151 if (NOT TARGET llvm_gtest)
152 add_library(llvm_gtest
153 ${UNITTEST_DIR}/googletest/src/gtest-all.cc
154 ${UNITTEST_DIR}/googlemock/src/gmock-all.cc
156 target_include_directories(llvm_gtest
158 "${UNITTEST_DIR}/googletest/include"
159 "${UNITTEST_DIR}/googlemock/include"
162 "${UNITTEST_DIR}/googletest"
163 "${UNITTEST_DIR}/googlemock"
165 find_package(Threads)
166 target_link_libraries(llvm_gtest PUBLIC Threads::Threads)
167 add_library(llvm_gtest_main ${UNITTEST_DIR}/UnitTestMain/TestMain.cpp)
168 target_link_libraries(gtest_main PUBLIC llvm_gtest)
170 set(FLANG_GTEST_AVAIL 1)
173 "Unit-tests will be skipped as LLVM install does not include google-test related headers and libraries.")
174 set(FLANG_GTEST_AVAIL 0)
177 if (FLANG_GTEST_AVAIL)
178 add_custom_target(check-all DEPENDS check-flang FlangUnitTests)
180 add_custom_target(check-all DEPENDS check-flang )
183 add_custom_target(doxygen ALL)
187 option(FLANG_INCLUDE_TESTS
188 "Generate build targets for the Flang unit tests."
189 ${LLVM_INCLUDE_TESTS})
190 set(FLANG_GTEST_AVAIL 1)
192 if(FLANG_STANDALONE_BUILD)
193 set(FLANG_BINARY_DIR ${CMAKE_BINARY_DIR}/tools/flang)
195 set(FLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
198 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
199 set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --src-root
200 set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir
201 set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
202 set(MLIR_TABLEGEN_EXE $<TARGET_FILE:mlir-tblgen>)
203 include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
204 include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
206 set(FLANG_INTRINSIC_MODULES_DIR ${CMAKE_BINARY_DIR}/include/flang)
207 set(FLANG_INCLUDE_DIR ${FLANG_BINARY_DIR}/include)
209 # TODO: Remove when libclangDriver is lifted out of Clang
210 if(FLANG_STANDALONE_BUILD)
211 set(CLANG_INCLUDE_DIR ${CLANG_INCLUDE_DIRS} )
212 # No need to specify TableGen output dir as that's embedded in CLANG_DIR
214 set(CLANG_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../clang/include )
215 # Specify TableGen output dir for things like DiagnosticCommonKinds.inc,
216 # DiagnosticDriverKinds.inc (required for reporting diagnostics)
217 set(CLANG_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/clang/include)
218 include_directories(SYSTEM ${CLANG_TABLEGEN_OUTPUT_DIR})
220 include_directories(SYSTEM ${CLANG_INCLUDE_DIR})
222 # tco tool and FIR lib output directories
223 if(FLANG_STANDALONE_BUILD)
224 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
225 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
227 # Always build tco tool
228 set(LLVM_BUILD_TOOLS ON)
230 include_directories(BEFORE
231 ${FLANG_BINARY_DIR}/include
232 ${FLANG_SOURCE_DIR}/include)
234 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
235 set(LLVM_COMMON_CMAKE_UTILS ${FLANG_SOURCE_DIR}/../cmake)
238 # Add Flang-centric modules to cmake path.
239 list(INSERT CMAKE_MODULE_PATH 0
240 "${FLANG_SOURCE_DIR}/cmake/modules"
241 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
245 if (NOT DEFAULT_SYSROOT)
246 set(DEFAULT_SYSROOT "" CACHE PATH
247 "The <path> to use for the system root for all compiler invocations (--sysroot=<path>).")
250 if (NOT ENABLE_LINKER_BUILD_ID)
251 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
254 set(FLANG_DEFAULT_LINKER "" CACHE STRING
255 "Default linker to use (linker name or absolute path, empty for platform default)")
257 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
258 "Default Fortran runtime library to use (\"libFortranRuntime\"), leave empty for platform default.")
260 if (NOT(FLANG_DEFAULT_RTLIB STREQUAL ""))
261 message(WARNING "Resetting Flang's default runtime library to use platform default.")
262 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
263 "Default runtime library to use (empty for platform default)" FORCE)
268 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
271 if (NOT DEFINED FLANG_VERSION_MAJOR)
272 set(FLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
275 if (NOT DEFINED FLANG_VERSION_MINOR)
276 set(FLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
279 if (NOT DEFINED FLANG_VERSION_PATCHLEVEL)
280 set(FLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
283 # Unlike PACKAGE_VERSION, FLANG_VERSION does not include LLVM_VERSION_SUFFIX.
284 set(FLANG_VERSION "${FLANG_VERSION_MAJOR}.${FLANG_VERSION_MINOR}.${FLANG_VERSION_PATCHLEVEL}")
285 message(STATUS "Flang version: ${FLANG_VERSION}")
286 # Flang executable version information
287 set(FLANG_EXECUTABLE_VERSION
288 "${FLANG_VERSION_MAJOR}" CACHE STRING
289 "Major version number to appended to the flang executable name.")
290 set(LIBFLANG_LIBRARY_VERSION
291 "${FLANG_VERSION_MAJOR}" CACHE STRING
292 "Major version number to appended to the libflang library.")
294 mark_as_advanced(FLANG_EXECUTABLE_VERSION LIBFLANG_LIBRARY_VERSION)
296 set(FLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
297 "Vendor-specific Flang version information.")
298 set(FLANG_VENDOR_UTI "org.llvm.flang" CACHE STRING
299 "Vendor-specific uti.")
302 add_definitions(-DFLANG_VENDOR="${FLANG_VENDOR} ")
305 set(FLANG_REPOSITORY_STRING "" CACHE STRING
306 "Vendor-specific text for showing the repository the source is taken from.")
307 if (FLANG_REPOSITORY_STRING)
308 add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}")
311 include(TestBigEndian)
312 test_big_endian(IS_BIGENDIAN)
314 add_compile_definitions(FLANG_BIG_ENDIAN=1)
316 add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
319 # Configure Flang's Version.inc file.
321 ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in
322 ${CMAKE_CURRENT_BINARY_DIR}/include/flang/Version.inc)
323 # Configure Flang's version info header file.
325 ${FLANG_SOURCE_DIR}/include/flang/Config/config.h.cmake
326 ${FLANG_BINARY_DIR}/include/flang/Config/config.h)
328 if (FLANG_ENABLE_WERROR)
329 # The following is taken from llvm/cmake/modules/HandleLLVMOptions.cmake
330 # Keep this up-to-date with that file
332 append("/WX" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
334 if ( LLVM_COMPILER_IS_GCC_COMPATIBLE )
335 append("-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
336 append("-Wno-error" CMAKE_REQUIRED_FLAGS)
337 endif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
340 # Builtin check_cxx_compiler_flag doesn't seem to work correctly
341 macro(check_compiler_flag flag resultVar)
342 unset(${resultVar} CACHE)
343 check_cxx_compiler_flag("${flag}" ${resultVar})
346 check_compiler_flag("-Werror -Wno-deprecated-copy" CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
347 if (CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
348 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-copy")
350 check_compiler_flag("-Wstring-conversion" CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
351 if (CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
352 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-string-conversion")
355 # Add appropriate flags for GCC
356 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
358 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
359 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -fno-semantic-interposition")
361 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument -Wstring-conversion \
362 -Wcovered-switch-default")
365 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
366 if (CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
367 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types")
370 # Add to build type flags.
371 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUGF18")
372 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -DCHECK=\"(void)\"")
374 # Building shared libraries is bad for performance with GCC by default
375 # due to the need to preserve the right to override external entry points
376 if (BUILD_SHARED_LIBS AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
377 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-semantic-interposition")
382 list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS)
384 # Determine HOST_LINK_VERSION on Darwin.
385 set(HOST_LINK_VERSION)
389 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
390 RESULT_VARIABLE HAD_ERROR
391 OUTPUT_VARIABLE LD_V_OUTPUT)
393 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
394 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
395 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
396 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
399 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
403 include(CMakeParseArguments)
407 add_subdirectory(include)
408 add_subdirectory(lib)
409 add_subdirectory(cmake/modules)
411 option(FLANG_BUILD_TOOLS
412 "Build the Flang tools. If OFF, just generate build targets." ON)
413 if (FLANG_BUILD_TOOLS)
414 add_subdirectory(tools)
416 add_subdirectory(runtime)
418 option(FLANG_BUILD_EXAMPLES "Build Flang example programs by default." OFF)
419 if (FLANG_BUILD_EXAMPLES AND FLANG_STANDALONE_BUILD)
420 message(FATAL_ERROR "Examples are not supported in out-of-tree builds.")
422 add_subdirectory(examples)
424 if (FLANG_INCLUDE_TESTS)
425 add_subdirectory(test)
426 if (FLANG_GTEST_AVAIL)
427 add_subdirectory(unittests)
431 option(FLANG_INCLUDE_DOCS "Generate build targets for the Flang docs."
432 ${LLVM_INCLUDE_DOCS})
433 if (FLANG_INCLUDE_DOCS)
434 add_subdirectory(docs)
437 # Custom target to install Flang libraries.
438 add_custom_target(flang-libraries)
439 set_target_properties(flang-libraries PROPERTIES FOLDER "Misc")
441 if (NOT LLVM_ENABLE_IDE)
442 add_llvm_install_targets(install-flang-libraries
443 DEPENDS flang-libraries
444 COMPONENT flang-libraries)
447 get_property(FLANG_LIBS GLOBAL PROPERTY FLANG_LIBS)
449 list(REMOVE_DUPLICATES FLANG_LIBS)
450 foreach(lib ${FLANG_LIBS})
451 add_dependencies(flang-libraries ${lib})
452 if (NOT LLVM_ENABLE_IDE)
453 add_dependencies(install-flang-libraries install-${lib})
458 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
459 install(DIRECTORY include/flang
460 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
461 COMPONENT flang-headers
467 PATTERN "config.h" EXCLUDE
468 PATTERN ".git" EXCLUDE
469 PATTERN "CMakeFiles" EXCLUDE)
471 install(DIRECTORY ${FLANG_INCLUDE_DIR}/flang
472 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
473 COMPONENT flang-headers