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 include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
203 include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
206 set(FLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
207 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
208 mark_as_advanced(FLANG_TOOLS_INSTALL_DIR)
210 set(FLANG_INTRINSIC_MODULES_DIR ${CMAKE_BINARY_DIR}/include/flang)
211 set(FLANG_INCLUDE_DIR ${FLANG_BINARY_DIR}/include)
213 # TODO: Remove when libclangDriver is lifted out of Clang
214 if(FLANG_STANDALONE_BUILD)
215 set(CLANG_INCLUDE_DIR ${CLANG_INCLUDE_DIRS} )
216 # No need to specify TableGen output dir as that's embedded in CLANG_DIR
218 set(CLANG_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../clang/include )
219 # Specify TableGen output dir for things like DiagnosticCommonKinds.inc,
220 # DiagnosticDriverKinds.inc (required for reporting diagnostics)
221 set(CLANG_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/clang/include)
222 include_directories(SYSTEM ${CLANG_TABLEGEN_OUTPUT_DIR})
224 include_directories(SYSTEM ${CLANG_INCLUDE_DIR})
226 # tco tool and FIR lib output directories
227 if(FLANG_STANDALONE_BUILD)
228 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
229 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
231 # Always build tco tool
232 set(LLVM_BUILD_TOOLS ON)
234 include_directories(BEFORE
235 ${FLANG_BINARY_DIR}/include
236 ${FLANG_SOURCE_DIR}/include)
238 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
239 set(LLVM_COMMON_CMAKE_UTILS ${FLANG_SOURCE_DIR}/../cmake)
242 # Add Flang-centric modules to cmake path.
243 list(INSERT CMAKE_MODULE_PATH 0
244 "${FLANG_SOURCE_DIR}/cmake/modules"
245 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
249 if (NOT DEFAULT_SYSROOT)
250 set(DEFAULT_SYSROOT "" CACHE PATH
251 "The <path> to use for the system root for all compiler invocations (--sysroot=<path>).")
254 if (NOT ENABLE_LINKER_BUILD_ID)
255 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
258 set(FLANG_DEFAULT_LINKER "" CACHE STRING
259 "Default linker to use (linker name or absolute path, empty for platform default)")
261 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
262 "Default Fortran runtime library to use (\"libFortranRuntime\"), leave empty for platform default.")
264 if (NOT(FLANG_DEFAULT_RTLIB STREQUAL ""))
265 message(WARNING "Resetting Flang's default runtime library to use platform default.")
266 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
267 "Default runtime library to use (empty for platform default)" FORCE)
272 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
275 if (NOT DEFINED FLANG_VERSION_MAJOR)
276 set(FLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
279 if (NOT DEFINED FLANG_VERSION_MINOR)
280 set(FLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
283 if (NOT DEFINED FLANG_VERSION_PATCHLEVEL)
284 set(FLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
287 # Unlike PACKAGE_VERSION, FLANG_VERSION does not include LLVM_VERSION_SUFFIX.
288 set(FLANG_VERSION "${FLANG_VERSION_MAJOR}.${FLANG_VERSION_MINOR}.${FLANG_VERSION_PATCHLEVEL}")
289 message(STATUS "Flang version: ${FLANG_VERSION}")
290 # Flang executable version information
291 set(FLANG_EXECUTABLE_VERSION
292 "${FLANG_VERSION_MAJOR}" CACHE STRING
293 "Major version number to appended to the flang executable name.")
294 set(LIBFLANG_LIBRARY_VERSION
295 "${FLANG_VERSION_MAJOR}" CACHE STRING
296 "Major version number to appended to the libflang library.")
298 mark_as_advanced(FLANG_EXECUTABLE_VERSION LIBFLANG_LIBRARY_VERSION)
300 set(FLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
301 "Vendor-specific Flang version information.")
302 set(FLANG_VENDOR_UTI "org.llvm.flang" CACHE STRING
303 "Vendor-specific uti.")
306 add_definitions(-DFLANG_VENDOR="${FLANG_VENDOR} ")
309 set(FLANG_REPOSITORY_STRING "" CACHE STRING
310 "Vendor-specific text for showing the repository the source is taken from.")
311 if (FLANG_REPOSITORY_STRING)
312 add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}")
315 include(TestBigEndian)
316 test_big_endian(IS_BIGENDIAN)
318 add_compile_definitions(FLANG_BIG_ENDIAN=1)
320 add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
323 # Configure Flang's Version.inc file.
325 ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in
326 ${CMAKE_CURRENT_BINARY_DIR}/include/flang/Version.inc)
327 # Configure Flang's version info header file.
329 ${FLANG_SOURCE_DIR}/include/flang/Config/config.h.cmake
330 ${FLANG_BINARY_DIR}/include/flang/Config/config.h)
332 if (FLANG_ENABLE_WERROR)
333 # The following is taken from llvm/cmake/modules/HandleLLVMOptions.cmake
334 # Keep this up-to-date with that file
336 append("/WX" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
338 if ( LLVM_COMPILER_IS_GCC_COMPATIBLE )
339 append("-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
340 append("-Wno-error" CMAKE_REQUIRED_FLAGS)
341 endif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
344 # Builtin check_cxx_compiler_flag doesn't seem to work correctly
345 macro(check_compiler_flag flag resultVar)
346 unset(${resultVar} CACHE)
347 check_cxx_compiler_flag("${flag}" ${resultVar})
350 check_compiler_flag("-Werror -Wno-deprecated-copy" CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
351 if (CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
352 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-copy")
354 check_compiler_flag("-Wstring-conversion" CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
355 if (CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
356 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-string-conversion")
358 check_compiler_flag("-Wno-ctad-maybe-unsupported" CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG)
359 if (CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG)
360 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-ctad-maybe-unsupported")
363 # Add appropriate flags for GCC
364 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
366 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
367 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -fno-semantic-interposition")
369 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument -Wstring-conversion \
370 -Wcovered-switch-default")
373 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
374 if (CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
375 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types")
378 # Add to build type flags.
379 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUGF18")
380 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -DCHECK=\"(void)\"")
382 # Building shared libraries is bad for performance with GCC by default
383 # due to the need to preserve the right to override external entry points
384 if (BUILD_SHARED_LIBS AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
385 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-semantic-interposition")
390 list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS)
392 # Determine HOST_LINK_VERSION on Darwin.
393 set(HOST_LINK_VERSION)
397 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
398 RESULT_VARIABLE HAD_ERROR
399 OUTPUT_VARIABLE LD_V_OUTPUT)
401 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
402 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
403 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
404 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
407 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
411 include(CMakeParseArguments)
415 add_subdirectory(include)
416 add_subdirectory(lib)
417 add_subdirectory(cmake/modules)
419 option(FLANG_BUILD_TOOLS
420 "Build the Flang tools. If OFF, just generate build targets." ON)
421 if (FLANG_BUILD_TOOLS)
422 add_subdirectory(tools)
424 add_subdirectory(runtime)
426 option(FLANG_BUILD_EXAMPLES "Build Flang example programs by default." OFF)
427 if (FLANG_BUILD_EXAMPLES AND FLANG_STANDALONE_BUILD)
428 message(FATAL_ERROR "Examples are not supported in out-of-tree builds.")
430 add_subdirectory(examples)
432 if (FLANG_INCLUDE_TESTS)
433 add_subdirectory(test)
434 if (FLANG_GTEST_AVAIL)
435 add_subdirectory(unittests)
439 option(FLANG_INCLUDE_DOCS "Generate build targets for the Flang docs."
440 ${LLVM_INCLUDE_DOCS})
441 if (FLANG_INCLUDE_DOCS)
442 add_subdirectory(docs)
445 # Custom target to install Flang libraries.
446 add_custom_target(flang-libraries)
447 set_target_properties(flang-libraries PROPERTIES FOLDER "Misc")
449 if (NOT LLVM_ENABLE_IDE)
450 add_llvm_install_targets(install-flang-libraries
451 DEPENDS flang-libraries
452 COMPONENT flang-libraries)
455 get_property(FLANG_LIBS GLOBAL PROPERTY FLANG_LIBS)
457 list(REMOVE_DUPLICATES FLANG_LIBS)
458 foreach(lib ${FLANG_LIBS})
459 add_dependencies(flang-libraries ${lib})
460 if (NOT LLVM_ENABLE_IDE)
461 add_dependencies(install-flang-libraries install-${lib})
466 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
467 install(DIRECTORY include/flang
468 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
469 COMPONENT flang-headers
475 PATTERN "config.h" EXCLUDE
476 PATTERN ".git" EXCLUDE
477 PATTERN "CMakeFiles" EXCLUDE)
479 install(DIRECTORY ${FLANG_INCLUDE_DIR}/flang
480 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
481 COMPONENT flang-headers