1 cmake_minimum_required(VERSION 3.20.0)
3 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
4 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
6 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
9 set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
11 # Flang requires C++17.
12 set(CMAKE_CXX_STANDARD 17)
13 set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
14 set(CMAKE_CXX_EXTENSIONS OFF)
17 # To match with the flang guidelines we currently disable
18 # building with exception support in core LLVM.
19 message(FATAL_ERROR "Flang does not currently support building with \
20 LLVM exceptions enabled. Please disable LLVM_ENABLE_EH when building \
24 set(FLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
26 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE)
27 message(FATAL_ERROR "In-source builds are not allowed. \
28 Please create a directory and run cmake from there,\
29 passing the path to this source directory as the last argument.\
30 This process created the file `CMakeCache.txt' and the directory\
31 `CMakeFiles'. Please delete them.")
34 option(FLANG_ENABLE_WERROR "Fail and stop building flang if a warning is triggered." OFF)
36 # Check for a standalone build and configure as appropriate from
38 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
39 message("Building Flang as a standalone project.")
41 set(FLANG_STANDALONE_BUILD ON)
43 set(FLANG_STANDALONE_BUILD OFF)
46 # Must go below project(..)
47 include(GNUInstallDirs)
49 # MSVC + clang-cl build requires clang_rt.builtin.${target} library
50 if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES Clang)
51 include(HandleCompilerRT)
52 find_compiler_rt_library(builtins CLANG_RT_BUILTINS_LIBRARY)
53 get_filename_component(LIBDIR "${CLANG_RT_BUILTINS_LIBRARY}" DIRECTORY)
54 if (IS_DIRECTORY "${LIBDIR}")
55 link_libraries(${CLANG_RT_BUILTINS_LIBRARY})
59 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
60 message(FATAL_ERROR "flang isn't supported on 32 bit CPUs")
63 if (FLANG_STANDALONE_BUILD)
64 set(FLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
65 set(CMAKE_INCLUDE_CURRENT_DIR ON)
67 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
68 CACHE BOOL "Enable assertions")
69 # Assertions follow llvm's configuration.
70 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
73 # We need a pre-built/installed version of LLVM.
74 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
75 # If the user specifies a relative path to LLVM_DIR, the calls to include
76 # LLVM modules fail. Append the absolute path to LLVM_DIR instead.
77 get_filename_component(LLVM_DIR_ABSOLUTE ${LLVM_DIR} REALPATH)
78 list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR_ABSOLUTE})
80 # Users might specify a path to CLANG_DIR that's:
82 # * a path relative to the path of this script.
83 # Append the absolute path to CLANG_DIR so that find_package works in both
85 get_filename_component(
89 ${CMAKE_CURRENT_SOURCE_DIR})
90 list(APPEND CMAKE_MODULE_PATH ${CLANG_DIR_ABSOLUTE})
92 # TODO: Remove when libclangDriver is lifted out of Clang
93 find_package(Clang REQUIRED PATHS "${CLANG_DIR_ABSOLUTE}" NO_DEFAULT_PATH)
95 message(FATAL_ERROR "Failed to find Clang")
98 # If LLVM links to zlib we need the imported targets so we can too.
100 find_package(ZLIB REQUIRED)
102 option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
103 if(CMAKE_COMPILER_IS_GNUCXX)
104 set(USE_NO_MAYBE_UNINITIALIZED 1)
107 include(CMakeParseArguments)
109 include(HandleLLVMOptions)
110 include(VersionFromVCS)
111 include(GetErrcMessages)
116 find_package(MLIR REQUIRED CONFIG)
117 # Use SYSTEM for the same reasons as for LLVM includes
118 include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
119 # If the user specifies a relative path to MLIR_DIR, the calls to include
120 # MLIR modules fail. Append the absolute path to MLIR_DIR instead.
121 get_filename_component(MLIR_DIR_ABSOLUTE ${MLIR_DIR} REALPATH)
122 list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR_ABSOLUTE})
124 find_program(MLIR_TABLEGEN_EXE "mlir-tblgen" ${LLVM_TOOLS_BINARY_DIR}
127 option(LLVM_INSTALL_TOOLCHAIN_ONLY
128 "Only include toolchain files in the 'install' target." OFF)
129 option(LLVM_FORCE_USE_OLD_TOOLCHAIN
130 "Set to ON to force using an old, unsupported host toolchain." OFF)
133 # Add LLVM include files as if they were SYSTEM because there are complex unused
134 # parameter issues that may or may not appear depending on the environments and
135 # compilers (ifdefs are involved). This allows warnings from LLVM headers to be
136 # ignored while keeping -Wunused-parameter a fatal error inside f18 code base.
137 # This may have to be fine-tuned if flang headers are consider part of this
138 # LLVM_INCLUDE_DIRS when merging in the monorepo (Warning from flang headers
139 # should not be suppressed).
140 include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
141 add_definitions(${LLVM_DEFINITIONS})
143 # LLVM's cmake configuration files currently sneak in a c++11 flag.
144 # We look for it here and remove it from Flang's compile flags to
145 # avoid some mixed compilation flangs (e.g. -std=c++11 ... -std=c++17).
146 if (DEFINED LLVM_CXX_STD)
147 message("LLVM configuration set a C++ standard: ${LLVM_CXX_STD}")
148 if (NOT LLVM_CXX_STD EQUAL "c++17")
149 message("Flang: Overriding LLVM's 'cxx_std' setting...")
150 message(" removing '-std=${LLVM_CXX_STD}'")
151 message(" CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'")
152 string(REPLACE " -std=${LLVM_CXX_STD}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
153 message(" [NEW] CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'")
157 link_directories("${LLVM_LIBRARY_DIR}")
159 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
160 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
161 ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
162 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
163 ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
165 set(LLVM_EXTERNAL_LIT "${LLVM_TOOLS_BINARY_DIR}/llvm-lit" CACHE STRING "Command used to spawn lit")
167 option(FLANG_INCLUDE_TESTS
168 "Generate build targets for the Flang unit tests."
171 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
173 #Handle unittests when out-of-tree
174 set(FLANG_GTEST_AVAIL 0)
175 if (FLANG_INCLUDE_TESTS)
176 set(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest)
177 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
178 if (NOT TARGET llvm_gtest)
179 add_subdirectory(${UNITTEST_DIR} third-party/unittest)
181 set(FLANG_GTEST_AVAIL 1)
184 "Unit-tests will be skipped as LLVM install does not include google-test related headers and libraries.")
185 set(FLANG_GTEST_AVAIL 0)
188 if (FLANG_GTEST_AVAIL)
189 add_custom_target(check-all DEPENDS check-flang FlangUnitTests)
191 add_custom_target(check-all DEPENDS check-flang )
194 add_custom_target(doxygen ALL)
198 option(FLANG_INCLUDE_TESTS
199 "Generate build targets for the Flang unit tests."
200 ${LLVM_INCLUDE_TESTS})
201 set(FLANG_GTEST_AVAIL 1)
203 if(FLANG_STANDALONE_BUILD)
204 set(FLANG_BINARY_DIR ${CMAKE_BINARY_DIR}/tools/flang)
206 set(FLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
209 set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir ) # --src-root
210 set(MLIR_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include ) # --includedir
211 set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
212 include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
213 include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
216 set(FLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
217 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
218 mark_as_advanced(FLANG_TOOLS_INSTALL_DIR)
220 set(FLANG_INTRINSIC_MODULES_DIR ${CMAKE_BINARY_DIR}/include/flang)
221 set(FLANG_INCLUDE_DIR ${FLANG_BINARY_DIR}/include)
223 # TODO: Remove when libclangDriver is lifted out of Clang
224 if(FLANG_STANDALONE_BUILD)
225 set(CLANG_INCLUDE_DIR ${CLANG_INCLUDE_DIRS} )
226 # No need to specify TableGen output dir as that's embedded in CLANG_DIR
228 set(CLANG_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../clang/include )
229 # Specify TableGen output dir for things like DiagnosticCommonKinds.inc,
230 # DiagnosticDriverKinds.inc (required for reporting diagnostics)
231 set(CLANG_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/clang/include)
232 include_directories(SYSTEM ${CLANG_TABLEGEN_OUTPUT_DIR})
234 include_directories(SYSTEM ${CLANG_INCLUDE_DIR})
236 # tco tool and FIR lib output directories
237 if(FLANG_STANDALONE_BUILD)
238 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
239 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
241 # Always build tco tool
242 set(LLVM_BUILD_TOOLS ON)
244 include_directories(BEFORE
245 ${FLANG_BINARY_DIR}/include
246 ${FLANG_SOURCE_DIR}/include)
248 # Add Flang-centric modules to cmake path.
249 list(INSERT CMAKE_MODULE_PATH 0
250 "${FLANG_SOURCE_DIR}/cmake/modules"
251 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
255 if (NOT DEFAULT_SYSROOT)
256 set(DEFAULT_SYSROOT "" CACHE PATH
257 "The <path> to use for the system root for all compiler invocations (--sysroot=<path>).")
260 if (NOT ENABLE_LINKER_BUILD_ID)
261 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
264 set(FLANG_DEFAULT_LINKER "" CACHE STRING
265 "Default linker to use (linker name or absolute path, empty for platform default)")
267 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
268 "Default Fortran runtime library to use (\"libFortranRuntime\"), leave empty for platform default.")
270 if (NOT(FLANG_DEFAULT_RTLIB STREQUAL ""))
271 message(WARNING "Resetting Flang's default runtime library to use platform default.")
272 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
273 "Default runtime library to use (empty for platform default)" FORCE)
278 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
279 if (NOT PACKAGE_VERSION)
280 set(PACKAGE_VERSION ${LLVM_VERSION_MAJOR})
284 if (NOT DEFINED FLANG_VERSION_MAJOR)
285 set(FLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
288 if (NOT DEFINED FLANG_VERSION_MINOR)
289 set(FLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
292 if (NOT DEFINED FLANG_VERSION_PATCHLEVEL)
293 set(FLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
296 # Unlike PACKAGE_VERSION, FLANG_VERSION does not include LLVM_VERSION_SUFFIX.
297 set(FLANG_VERSION "${FLANG_VERSION_MAJOR}.${FLANG_VERSION_MINOR}.${FLANG_VERSION_PATCHLEVEL}")
298 message(STATUS "Flang version: ${FLANG_VERSION}")
299 # Flang executable version information
300 set(FLANG_EXECUTABLE_VERSION
301 "${FLANG_VERSION_MAJOR}" CACHE STRING
302 "Major version number to appended to the flang executable name.")
303 set(LIBFLANG_LIBRARY_VERSION
304 "${FLANG_VERSION_MAJOR}" CACHE STRING
305 "Major version number to appended to the libflang library.")
307 mark_as_advanced(FLANG_EXECUTABLE_VERSION LIBFLANG_LIBRARY_VERSION)
309 set(FLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
310 "Vendor-specific Flang version information.")
311 set(FLANG_VENDOR_UTI "org.llvm.flang" CACHE STRING
312 "Vendor-specific uti.")
315 add_definitions(-DFLANG_VENDOR="${FLANG_VENDOR} ")
318 set(FLANG_REPOSITORY_STRING "" CACHE STRING
319 "Vendor-specific text for showing the repository the source is taken from.")
320 if (FLANG_REPOSITORY_STRING)
321 add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}")
324 include(TestBigEndian)
325 test_big_endian(IS_BIGENDIAN)
327 add_compile_definitions(FLANG_BIG_ENDIAN=1)
329 add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
332 # Configure Flang's Version.inc file.
334 ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in
335 ${CMAKE_CURRENT_BINARY_DIR}/include/flang/Version.inc)
336 # Configure Flang's version info header file.
338 ${FLANG_SOURCE_DIR}/include/flang/Config/config.h.cmake
339 ${FLANG_BINARY_DIR}/include/flang/Config/config.h)
341 if (FLANG_ENABLE_WERROR)
342 # The following is taken from llvm/cmake/modules/HandleLLVMOptions.cmake
343 # Keep this up-to-date with that file
345 append("/WX" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
347 if ( LLVM_COMPILER_IS_GCC_COMPATIBLE )
348 append("-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
349 append("-Wno-error" CMAKE_REQUIRED_FLAGS)
350 endif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
353 # Builtin check_cxx_compiler_flag doesn't seem to work correctly
354 macro(check_compiler_flag flag resultVar)
355 unset(${resultVar} CACHE)
356 check_cxx_compiler_flag("${flag}" ${resultVar})
359 check_compiler_flag("-Werror -Wno-deprecated-copy" CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
360 if (CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
361 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-copy")
363 check_compiler_flag("-Wstring-conversion" CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
364 if (CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
365 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-string-conversion")
367 check_compiler_flag("-Wno-ctad-maybe-unsupported" CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG)
368 if (CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG)
369 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-ctad-maybe-unsupported")
372 # Add appropriate flags for GCC
373 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
375 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
376 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -fno-semantic-interposition")
378 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument -Wstring-conversion \
379 -Wcovered-switch-default")
382 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
383 if (CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
384 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types")
387 # Add to build type flags.
388 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUGF18")
389 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -DCHECK=\"(void)\"")
391 # Building shared libraries is bad for performance with GCC by default
392 # due to the need to preserve the right to override external entry points
393 if (BUILD_SHARED_LIBS AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
394 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-semantic-interposition")
399 list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS)
401 # Determine HOST_LINK_VERSION on Darwin.
402 set(HOST_LINK_VERSION)
406 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
407 RESULT_VARIABLE HAD_ERROR
408 OUTPUT_VARIABLE LD_V_OUTPUT)
410 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
411 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
412 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
413 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
416 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
420 include(CMakeParseArguments)
423 if (FLANG_INCLUDE_TESTS)
424 add_compile_definitions(FLANG_INCLUDE_TESTS=1)
427 add_subdirectory(include)
428 add_subdirectory(lib)
429 add_subdirectory(cmake/modules)
431 option(FLANG_BUILD_TOOLS
432 "Build the Flang tools. If OFF, just generate build targets." ON)
433 if (FLANG_BUILD_TOOLS)
434 add_subdirectory(tools)
436 add_subdirectory(runtime)
438 if (LLVM_INCLUDE_EXAMPLES)
439 add_subdirectory(examples)
442 if (FLANG_INCLUDE_TESTS)
443 add_subdirectory(test)
444 if (FLANG_GTEST_AVAIL)
445 add_subdirectory(unittests)
449 option(FLANG_INCLUDE_DOCS "Generate build targets for the Flang docs."
450 ${LLVM_INCLUDE_DOCS})
451 if (FLANG_INCLUDE_DOCS)
452 add_subdirectory(docs)
455 # Custom target to install Flang libraries.
456 add_custom_target(flang-libraries)
457 set_target_properties(flang-libraries PROPERTIES FOLDER "Misc")
459 if (NOT LLVM_ENABLE_IDE)
460 add_llvm_install_targets(install-flang-libraries
461 DEPENDS flang-libraries
462 COMPONENT flang-libraries)
465 get_property(FLANG_LIBS GLOBAL PROPERTY FLANG_LIBS)
467 list(REMOVE_DUPLICATES FLANG_LIBS)
468 foreach(lib ${FLANG_LIBS})
469 add_dependencies(flang-libraries ${lib})
470 if (NOT LLVM_ENABLE_IDE)
471 add_dependencies(install-flang-libraries install-${lib})
476 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
477 install(DIRECTORY include/flang
478 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
479 COMPONENT flang-headers
485 PATTERN "config.h" EXCLUDE
486 PATTERN ".git" EXCLUDE
487 PATTERN "CMakeFiles" EXCLUDE)
489 install(DIRECTORY ${FLANG_INCLUDE_DIR}/flang
490 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
491 COMPONENT flang-headers
497 # Put ISO_Fortran_binding.h into the include files of the build area now
498 # so that we can run tests before installing
499 include(GetClangResourceDir)
500 get_clang_resource_dir(HEADER_BINARY_DIR PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/.. SUBDIR include)
502 ${FLANG_SOURCE_DIR}/include/flang/ISO_Fortran_binding.h
503 ${HEADER_BINARY_DIR}/ISO_Fortran_binding.h)
505 # And also install it into the install area
506 get_clang_resource_dir(HEADER_INSTALL_DIR SUBDIR include)
508 FILES include/flang/ISO_Fortran_binding.h
509 DESTINATION ${HEADER_INSTALL_DIR} )