blob: c8e75024823f2cc98f4dacc0bc9125f588e216fe [file] [log] [blame]
Mark de Wevercbaa3592023-05-24 16:12:321cmake_minimum_required(VERSION 3.20.0)
Tim Keithd45e6c72018-01-31 00:37:592
Michał Górny9dd01a52022-10-24 04:31:373if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
4 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
5endif()
6include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
7 NO_POLICY_SCOPE)
8
Raul Tambre480643a2021-01-10 12:11:359set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
Patrick McCormick6c16aa42020-02-25 23:22:1410
Patrick McCormick6c16aa42020-02-25 23:22:1411# Flang requires C++17.
12set(CMAKE_CXX_STANDARD 17)
13set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
14set(CMAKE_CXX_EXTENSIONS OFF)
15
Patrick McCormickf7e58c92023-04-26 19:49:5816if (LLVM_ENABLE_EH)
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 \
21 flang.")
22endif()
23
Patrick McCormick6c16aa42020-02-25 23:22:1424set(FLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
25
26if (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.")
Stephane Chauveau8f6f23f2018-02-19 13:28:1232endif()
Patrick McCormick6c16aa42020-02-25 23:22:1433
Isuru Fernando9017b9c2020-07-14 15:59:2634option(FLANG_ENABLE_WERROR "Fail and stop building flang if a warning is triggered." OFF)
Isuru Fernando3862cbb2020-06-08 14:08:3535
Slava Zakharina468d022024-02-20 20:33:0836# The out of tree builds of the compiler and the Fortran runtime
37# must use the same setting of FLANG_RUNTIME_F128_MATH_LIB
38# to be composable. Failure to synchronize this setting may result
39# in linking errors or fatal failures in F128 runtime functions.
40set(FLANG_RUNTIME_F128_MATH_LIB "" CACHE STRING
41 "Specifies the target library used for implementing IEEE-754 128-bit float \
42 math in F18 runtime, e.g. it might be libquadmath for targets where \
43 REAL(16) is mapped to __float128, or libm for targets where REAL(16) \
44 is mapped to long double, etc."
45 )
46
Patrick McCormick6c16aa42020-02-25 23:22:1447# Check for a standalone build and configure as appropriate from
48# there.
49if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
Isuru Fernando3862cbb2020-06-08 14:08:3550 message("Building Flang as a standalone project.")
51 project(Flang)
sameeran joshifa5d4162020-06-22 04:54:2852 set(FLANG_STANDALONE_BUILD ON)
John Ericsondf31ff12022-01-19 06:45:0753else()
54 set(FLANG_STANDALONE_BUILD OFF)
55endif()
Patrick McCormick6c16aa42020-02-25 23:22:1456
John Ericsondf31ff12022-01-19 06:45:0757# Must go below project(..)
58include(GNUInstallDirs)
59
Muhammad Omair Javaidc687c2c2022-11-30 11:28:1360# MSVC + clang-cl build requires clang_rt.builtin.${target} library
61if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES Clang)
62 include(HandleCompilerRT)
63 find_compiler_rt_library(builtins CLANG_RT_BUILTINS_LIBRARY)
64 get_filename_component(LIBDIR "${CLANG_RT_BUILTINS_LIBRARY}" DIRECTORY)
65 if (IS_DIRECTORY "${LIBDIR}")
66 link_libraries(${CLANG_RT_BUILTINS_LIBRARY})
67 endif()
68endif()
69
Sylvestre Ledru8e20cb62023-01-06 16:12:0370if(CMAKE_SIZEOF_VOID_P EQUAL 4)
71 message(FATAL_ERROR "flang isn't supported on 32 bit CPUs")
72endif()
73
John Ericsondf31ff12022-01-19 06:45:0774if (FLANG_STANDALONE_BUILD)
Patrick McCormick6c16aa42020-02-25 23:22:1475 set(FLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Hussain Kadhem287f2012023-06-03 13:13:0976 set(CMAKE_INCLUDE_CURRENT_DIR ON)
Patrick McCormick6c16aa42020-02-25 23:22:1477 if (NOT MSVC_IDE)
78 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
79 CACHE BOOL "Enable assertions")
80 # Assertions follow llvm's configuration.
81 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
peter klausler9702fca2018-04-27 19:37:4782 endif()
Patrick McCormick6c16aa42020-02-25 23:22:1483
Diana Picusd4b88ac2020-09-03 11:39:2984 # If the user specifies a relative path to LLVM_DIR, the calls to include
85 # LLVM modules fail. Append the absolute path to LLVM_DIR instead.
Slava Zakharina6a4d4a2024-04-19 15:51:2686 get_filename_component(LLVM_DIR_ABSOLUTE ${LLVM_DIR}
87 REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
Diana Picusd4b88ac2020-09-03 11:39:2988 list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR_ABSOLUTE})
Slava Zakharina6a4d4a2024-04-19 15:51:2689 # We need a pre-built/installed version of LLVM.
90 find_package(LLVM REQUIRED HINTS "${LLVM_DIR_ABSOLUTE}")
Patrick McCormick6c16aa42020-02-25 23:22:1491
Andrzej Warzynski7b73ca32021-07-12 08:44:3892 # Users might specify a path to CLANG_DIR that's:
93 # * a full path, or
94 # * a path relative to the path of this script.
95 # Append the absolute path to CLANG_DIR so that find_package works in both
96 # cases.
97 get_filename_component(
98 CLANG_DIR_ABSOLUTE
99 ${CLANG_DIR}
100 REALPATH
Slava Zakharina6a4d4a2024-04-19 15:51:26101 BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
Andrzej Warzynski7b73ca32021-07-12 08:44:38102 list(APPEND CMAKE_MODULE_PATH ${CLANG_DIR_ABSOLUTE})
Andrzej Warzynskiaf805682021-03-22 17:04:01103
Andrzej Warzynski7b73ca32021-07-12 08:44:38104 # TODO: Remove when libclangDriver is lifted out of Clang
105 find_package(Clang REQUIRED PATHS "${CLANG_DIR_ABSOLUTE}" NO_DEFAULT_PATH)
106 if (NOT Clang_FOUND)
107 message(FATAL_ERROR "Failed to find Clang")
Caroline Concatto257b2972020-09-11 09:17:31108 endif()
109
Patrick McCormick6c16aa42020-02-25 23:22:14110 # If LLVM links to zlib we need the imported targets so we can too.
111 if(LLVM_ENABLE_ZLIB)
112 find_package(ZLIB REQUIRED)
113 endif()
Isuru Fernando3862cbb2020-06-08 14:08:35114 option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
Tim Keitha9795b62020-08-11 14:20:13115 if(CMAKE_COMPILER_IS_GNUCXX)
116 set(USE_NO_MAYBE_UNINITIALIZED 1)
117 endif()
Patrick McCormick6c16aa42020-02-25 23:22:14118
119 include(CMakeParseArguments)
120 include(AddLLVM)
121 include(HandleLLVMOptions)
122 include(VersionFromVCS)
Andrzej Warzynski787c4432021-08-20 10:25:11123 include(GetErrcMessages)
Patrick McCormick6c16aa42020-02-25 23:22:14124
Andrzej Warzynski7b73ca32021-07-12 08:44:38125 include(AddClang)
Andrzej Warzynski42e89ab2020-10-14 16:40:29126
Sourabh Singh Tomar6a7deff2021-02-10 12:53:10127 include(TableGen)
Slava Zakharin215eee6042024-04-18 05:06:30128 # If the user specifies a relative path to MLIR_DIR, the calls to include
129 # MLIR modules fail. Append the absolute path to MLIR_DIR instead.
Slava Zakharina6a4d4a2024-04-19 15:51:26130 get_filename_component(MLIR_DIR_ABSOLUTE ${MLIR_DIR}
131 REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR})
Slava Zakharin215eee6042024-04-18 05:06:30132 list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR_ABSOLUTE})
Slava Zakharina6a4d4a2024-04-19 15:51:26133 find_package(MLIR REQUIRED CONFIG HINTS ${MLIR_DIR_ABSOLUTE})
134 # Use SYSTEM for the same reasons as for LLVM includes
135 include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
Sourabh Singh Tomar6a7deff2021-02-10 12:53:10136 include(AddMLIR)
137 find_program(MLIR_TABLEGEN_EXE "mlir-tblgen" ${LLVM_TOOLS_BINARY_DIR}
138 NO_DEFAULT_PATH)
Patrick McCormick6c16aa42020-02-25 23:22:14139
Patrick McCormick6c16aa42020-02-25 23:22:14140 option(LLVM_INSTALL_TOOLCHAIN_ONLY
141 "Only include toolchain files in the 'install' target." OFF)
Jonas Hahnfeld0b3c48d2023-05-17 08:28:58142 option(LLVM_FORCE_USE_OLD_TOOLCHAIN
Patrick McCormick6c16aa42020-02-25 23:22:14143 "Set to ON to force using an old, unsupported host toolchain." OFF)
144
145
146 # Add LLVM include files as if they were SYSTEM because there are complex unused
147 # parameter issues that may or may not appear depending on the environments and
148 # compilers (ifdefs are involved). This allows warnings from LLVM headers to be
149 # ignored while keeping -Wunused-parameter a fatal error inside f18 code base.
150 # This may have to be fine-tuned if flang headers are consider part of this
151 # LLVM_INCLUDE_DIRS when merging in the monorepo (Warning from flang headers
152 # should not be suppressed).
153 include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
154 add_definitions(${LLVM_DEFINITIONS})
155
156 # LLVM's cmake configuration files currently sneak in a c++11 flag.
157 # We look for it here and remove it from Flang's compile flags to
158 # avoid some mixed compilation flangs (e.g. -std=c++11 ... -std=c++17).
159 if (DEFINED LLVM_CXX_STD)
160 message("LLVM configuration set a C++ standard: ${LLVM_CXX_STD}")
161 if (NOT LLVM_CXX_STD EQUAL "c++17")
162 message("Flang: Overriding LLVM's 'cxx_std' setting...")
163 message(" removing '-std=${LLVM_CXX_STD}'")
164 message(" CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'")
165 string(REPLACE " -std=${LLVM_CXX_STD}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
166 message(" [NEW] CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'")
167 endif()
168 endif()
169
170 link_directories("${LLVM_LIBRARY_DIR}")
171
172 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
173 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
John Ericsone941b032022-08-19 02:44:46174 ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
Patrick McCormick6c16aa42020-02-25 23:22:14175 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
John Ericsone941b032022-08-19 02:44:46176 ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
Patrick McCormick6c16aa42020-02-25 23:22:14177
Patrick McCormick6c16aa42020-02-25 23:22:14178 set(LLVM_EXTERNAL_LIT "${LLVM_TOOLS_BINARY_DIR}/llvm-lit" CACHE STRING "Command used to spawn lit")
179
180 option(FLANG_INCLUDE_TESTS
181 "Generate build targets for the Flang unit tests."
182 ON)
sameeran joshi93f602b2020-06-02 17:15:44183
Andrzej Warzynski787c4432021-08-20 10:25:11184 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
185
sameeran joshi93f602b2020-06-02 17:15:44186#Handle unittests when out-of-tree
sameeran joshi93f602b2020-06-02 17:15:44187 set(FLANG_GTEST_AVAIL 0)
188 if (FLANG_INCLUDE_TESTS)
Michał Górny58d19882022-11-23 12:19:47189 set(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest)
sameeran joshi93f602b2020-06-02 17:15:44190 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
Stella Laurenzo38151a02022-03-04 20:08:04191 if (NOT TARGET llvm_gtest)
Michał Górny02a2e182022-11-23 15:07:03192 add_subdirectory(${UNITTEST_DIR} third-party/unittest)
sameeran joshi93f602b2020-06-02 17:15:44193 endif()
194 set(FLANG_GTEST_AVAIL 1)
195 else()
196 message(WARNING
197 "Unit-tests will be skipped as LLVM install does not include google-test related headers and libraries.")
198 set(FLANG_GTEST_AVAIL 0)
199 endif()
200 endif()
201 if (FLANG_GTEST_AVAIL)
202 add_custom_target(check-all DEPENDS check-flang FlangUnitTests)
203 else()
David Spickettffc67bb2023-10-02 09:01:11204 add_custom_target(check-all DEPENDS check-flang )
sameeran joshi93f602b2020-06-02 17:15:44205 endif()
sameeran joshi807fe052020-04-27 09:22:36206 if (LLVM_BUILD_DOCS)
207 add_custom_target(doxygen ALL)
208 endif()
209
Patrick McCormick6c16aa42020-02-25 23:22:14210else()
211 option(FLANG_INCLUDE_TESTS
212 "Generate build targets for the Flang unit tests."
213 ${LLVM_INCLUDE_TESTS})
sameeran joshi93f602b2020-06-02 17:15:44214 set(FLANG_GTEST_AVAIL 1)
215
Andrzej Warzynski621681e2020-08-01 19:53:27216 if(FLANG_STANDALONE_BUILD)
217 set(FLANG_BINARY_DIR ${CMAKE_BINARY_DIR}/tools/flang)
218 else()
219 set(FLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
220 endif()
221
Renaud-Ke2dd6332022-11-02 23:02:04222 set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir ) # --src-root
223 set(MLIR_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include ) # --includedir
Sourabh Singh Tomar6a7deff2021-02-10 12:53:10224 set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
Sourabh Singh Tomar6a7deff2021-02-10 12:53:10225 include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
226 include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
peter klausler9702fca2018-04-27 19:37:47227endif()
John Ericson07b74982022-06-11 06:11:59228
229set(FLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
230 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
231mark_as_advanced(FLANG_TOOLS_INSTALL_DIR)
232
Tim Keith8e1c09e2021-03-15 15:02:58233set(FLANG_INTRINSIC_MODULES_DIR ${CMAKE_BINARY_DIR}/include/flang)
Renaud-K1e116162021-05-10 23:41:29234set(FLANG_INCLUDE_DIR ${FLANG_BINARY_DIR}/include)
Tim Keithe8707d32018-02-14 23:29:18235
Andrzej Warzynski7b73ca32021-07-12 08:44:38236# TODO: Remove when libclangDriver is lifted out of Clang
237if(FLANG_STANDALONE_BUILD)
238 set(CLANG_INCLUDE_DIR ${CLANG_INCLUDE_DIRS} )
239 # No need to specify TableGen output dir as that's embedded in CLANG_DIR
240else()
241 set(CLANG_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../clang/include )
242 # Specify TableGen output dir for things like DiagnosticCommonKinds.inc,
243 # DiagnosticDriverKinds.inc (required for reporting diagnostics)
244 set(CLANG_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/clang/include)
245 include_directories(SYSTEM ${CLANG_TABLEGEN_OUTPUT_DIR})
Caroline Concatto257b2972020-09-11 09:17:31246endif()
Andrzej Warzynski7b73ca32021-07-12 08:44:38247include_directories(SYSTEM ${CLANG_INCLUDE_DIR})
Caroline Concatto257b2972020-09-11 09:17:31248
Sourabh Singh Tomar6a7deff2021-02-10 12:53:10249# tco tool and FIR lib output directories
250if(FLANG_STANDALONE_BUILD)
251 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
252 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
Tim Keith1c9f8d42019-03-13 19:41:15253endif()
Sourabh Singh Tomar6a7deff2021-02-10 12:53:10254# Always build tco tool
255set(LLVM_BUILD_TOOLS ON)
Stephane Chauveau8f6f23f2018-02-19 13:28:12256
Patrick McCormick6c16aa42020-02-25 23:22:14257include_directories(BEFORE
258 ${FLANG_BINARY_DIR}/include
259 ${FLANG_SOURCE_DIR}/include)
peter klausler85ea49e2019-01-23 00:03:02260
Shoaib Meenai30b7dfa2021-05-12 19:59:23261# Add Flang-centric modules to cmake path.
John Ericson5c3347a2021-04-04 17:02:18262list(INSERT CMAKE_MODULE_PATH 0
263 "${FLANG_SOURCE_DIR}/cmake/modules"
John Ericsonb3af9fb2022-01-01 17:51:16264 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
John Ericson5c3347a2021-04-04 17:02:18265 )
Shoaib Meenai30b7dfa2021-05-12 19:59:23266include(AddFlang)
Patrick McCormick6c16aa42020-02-25 23:22:14267
268if (NOT DEFAULT_SYSROOT)
269 set(DEFAULT_SYSROOT "" CACHE PATH
270 "The <path> to use for the system root for all compiler invocations (--sysroot=<path>).")
Stephane Chauveau8f6f23f2018-02-19 13:28:12271endif()
272
Patrick McCormick6c16aa42020-02-25 23:22:14273if (NOT ENABLE_LINKER_BUILD_ID)
274 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
275endif()
276
277set(FLANG_DEFAULT_LINKER "" CACHE STRING
278 "Default linker to use (linker name or absolute path, empty for platform default)")
279
280set(FLANG_DEFAULT_RTLIB "" CACHE STRING
281 "Default Fortran runtime library to use (\"libFortranRuntime\"), leave empty for platform default.")
282
283if (NOT(FLANG_DEFAULT_RTLIB STREQUAL ""))
284 message(WARNING "Resetting Flang's default runtime library to use platform default.")
285 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
286 "Default runtime library to use (empty for platform default)" FORCE)
287endif()
288
Peter Steinfelde78e1f52023-10-20 16:00:51289
290
Patrick McCormick6c16aa42020-02-25 23:22:14291set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
Pete Steinfeldddd27472023-10-30 13:44:09292if (NOT PACKAGE_VERSION)
293 set(PACKAGE_VERSION ${LLVM_VERSION_MAJOR})
294endif()
Peter Steinfelde78e1f52023-10-20 16:00:51295
Patrick McCormick6c16aa42020-02-25 23:22:14296
297if (NOT DEFINED FLANG_VERSION_MAJOR)
298 set(FLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
299endif()
300
301if (NOT DEFINED FLANG_VERSION_MINOR)
302 set(FLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
303endif()
304
305if (NOT DEFINED FLANG_VERSION_PATCHLEVEL)
306 set(FLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
307endif()
308
309# Unlike PACKAGE_VERSION, FLANG_VERSION does not include LLVM_VERSION_SUFFIX.
Stephane Chauveau8f6f23f2018-02-19 13:28:12310set(FLANG_VERSION "${FLANG_VERSION_MAJOR}.${FLANG_VERSION_MINOR}.${FLANG_VERSION_PATCHLEVEL}")
Patrick McCormick6c16aa42020-02-25 23:22:14311message(STATUS "Flang version: ${FLANG_VERSION}")
312# Flang executable version information
313set(FLANG_EXECUTABLE_VERSION
314 "${FLANG_VERSION_MAJOR}" CACHE STRING
315 "Major version number to appended to the flang executable name.")
316set(LIBFLANG_LIBRARY_VERSION
317 "${FLANG_VERSION_MAJOR}" CACHE STRING
318 "Major version number to appended to the libflang library.")
Stephane Chauveau8f6f23f2018-02-19 13:28:12319
Patrick McCormick6c16aa42020-02-25 23:22:14320mark_as_advanced(FLANG_EXECUTABLE_VERSION LIBFLANG_LIBRARY_VERSION)
Stephane Chauveau8f6f23f2018-02-19 13:28:12321
Patrick McCormick6c16aa42020-02-25 23:22:14322set(FLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
323 "Vendor-specific Flang version information.")
324set(FLANG_VENDOR_UTI "org.llvm.flang" CACHE STRING
325 "Vendor-specific uti.")
Stephane Chauveau8f6f23f2018-02-19 13:28:12326
Patrick McCormick6c16aa42020-02-25 23:22:14327if (FLANG_VENDOR)
328 add_definitions(-DFLANG_VENDOR="${FLANG_VENDOR} ")
329endif()
peter klausler57f41862018-06-14 20:43:02330
Patrick McCormick6c16aa42020-02-25 23:22:14331set(FLANG_REPOSITORY_STRING "" CACHE STRING
332 "Vendor-specific text for showing the repository the source is taken from.")
333if (FLANG_REPOSITORY_STRING)
334 add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}")
335endif()
Stephane Chauveau8f6f23f2018-02-19 13:28:12336
Slava Zakharina468d022024-02-20 20:33:08337if (FLANG_RUNTIME_F128_MATH_LIB)
338 add_compile_definitions(
339 -DFLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}"
340 )
341endif()
342
Michael Kruse84e12582021-09-03 19:48:40343include(TestBigEndian)
344test_big_endian(IS_BIGENDIAN)
345if (IS_BIGENDIAN)
346 add_compile_definitions(FLANG_BIG_ENDIAN=1)
347else ()
348 add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
349endif ()
350
Patrick McCormick6c16aa42020-02-25 23:22:14351# Configure Flang's Version.inc file.
352configure_file(
353 ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in
354 ${CMAKE_CURRENT_BINARY_DIR}/include/flang/Version.inc)
355# Configure Flang's version info header file.
Stephane Chauveau8f6f23f2018-02-19 13:28:12356configure_file(
357 ${FLANG_SOURCE_DIR}/include/flang/Config/config.h.cmake
358 ${FLANG_BINARY_DIR}/include/flang/Config/config.h)
Patrick McCormick6c16aa42020-02-25 23:22:14359
Isuru Fernando3862cbb2020-06-08 14:08:35360if (FLANG_ENABLE_WERROR)
361 # The following is taken from llvm/cmake/modules/HandleLLVMOptions.cmake
362 # Keep this up-to-date with that file
363 if( MSVC )
364 append("/WX" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
365 endif()
366 if ( LLVM_COMPILER_IS_GCC_COMPATIBLE )
367 append("-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
368 append("-Wno-error" CMAKE_REQUIRED_FLAGS)
369 endif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
Isuru Fernando3862cbb2020-06-08 14:08:35370endif()
Patrick McCormick6c16aa42020-02-25 23:22:14371
372# Builtin check_cxx_compiler_flag doesn't seem to work correctly
373macro(check_compiler_flag flag resultVar)
374 unset(${resultVar} CACHE)
375 check_cxx_compiler_flag("${flag}" ${resultVar})
376endmacro()
377
378check_compiler_flag("-Werror -Wno-deprecated-copy" CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
379if (CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
Isuru Fernando3862cbb2020-06-08 14:08:35380 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-copy")
Patrick McCormick6c16aa42020-02-25 23:22:14381endif()
382check_compiler_flag("-Wstring-conversion" CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
383if (CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
Isuru Fernando3862cbb2020-06-08 14:08:35384 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-string-conversion")
Patrick McCormick6c16aa42020-02-25 23:22:14385endif()
David Blaikie357e99a2022-08-16 17:42:45386check_compiler_flag("-Wno-ctad-maybe-unsupported" CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG)
387if (CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG)
388 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-ctad-maybe-unsupported")
389endif()
Patrick McCormick6c16aa42020-02-25 23:22:14390
391# Add appropriate flags for GCC
392if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
393
394 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
395 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -fno-semantic-interposition")
396 else()
397 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument -Wstring-conversion \
398 -Wcovered-switch-default")
399 endif() # Clang.
400
401 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
402 if (CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
403 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types")
404 endif()
405
David Truby81bf1e22020-05-29 13:03:21406 # Add to build type flags.
Patrick McCormick6c16aa42020-02-25 23:22:14407 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUGF18")
David Truby81bf1e22020-05-29 13:03:21408 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -DCHECK=\"(void)\"")
Patrick McCormick6c16aa42020-02-25 23:22:14409
410 # Building shared libraries is bad for performance with GCC by default
411 # due to the need to preserve the right to override external entry points
412 if (BUILD_SHARED_LIBS AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
413 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-semantic-interposition")
414 endif()
415
416endif()
417
Leandro Lupori3149c932024-03-11 11:25:41418# Clang on Darwin enables non-POSIX extensions by default, which allows the
419# macro HUGE to leak out of <math.h> even when it is never directly included,
420# conflicting with Flang's HUGE symbols.
421# Set _POSIX_C_SOURCE to avoid including these extensions.
422if (APPLE)
423 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_POSIX_C_SOURCE=200809")
424endif()
425
Patrick McCormick6c16aa42020-02-25 23:22:14426list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS)
427
428# Determine HOST_LINK_VERSION on Darwin.
429set(HOST_LINK_VERSION)
430if (APPLE)
431 set(LD_V_OUTPUT)
432 execute_process(
433 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
434 RESULT_VARIABLE HAD_ERROR
435 OUTPUT_VARIABLE LD_V_OUTPUT)
436 if (NOT HAD_ERROR)
437 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
438 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
439 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
440 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
441 endif()
442 else()
443 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
444 endif()
445endif()
446
447include(CMakeParseArguments)
448include(AddFlang)
449
Renaud-Kba655842022-11-02 22:39:58450if (FLANG_INCLUDE_TESTS)
451 add_compile_definitions(FLANG_INCLUDE_TESTS=1)
452endif()
Andrzej Warzynskic1f068b2021-07-01 08:27:31453
Patrick McCormick6c16aa42020-02-25 23:22:14454add_subdirectory(include)
455add_subdirectory(lib)
456add_subdirectory(cmake/modules)
457
458option(FLANG_BUILD_TOOLS
459 "Build the Flang tools. If OFF, just generate build targets." ON)
460if (FLANG_BUILD_TOOLS)
461 add_subdirectory(tools)
462endif()
David Spickettffc67bb2023-10-02 09:01:11463add_subdirectory(runtime)
Stuart Ellisf52fc592021-08-12 10:42:08464
Slava Zakharin1c5d1212023-03-13 21:50:13465if (LLVM_INCLUDE_EXAMPLES)
466 add_subdirectory(examples)
Andrzej Warzynski8150c1b2021-08-12 16:11:54467endif()
Patrick McCormick6c16aa42020-02-25 23:22:14468
469if (FLANG_INCLUDE_TESTS)
Patrick McCormick6c16aa42020-02-25 23:22:14470 add_subdirectory(test)
sameeran joshi93f602b2020-06-02 17:15:44471 if (FLANG_GTEST_AVAIL)
472 add_subdirectory(unittests)
473 endif ()
Patrick McCormick6c16aa42020-02-25 23:22:14474endif()
475
sameeran joshi807fe052020-04-27 09:22:36476option(FLANG_INCLUDE_DOCS "Generate build targets for the Flang docs."
477 ${LLVM_INCLUDE_DOCS})
478if (FLANG_INCLUDE_DOCS)
479 add_subdirectory(docs)
480endif()
Patrick McCormick6c16aa42020-02-25 23:22:14481
482# Custom target to install Flang libraries.
483add_custom_target(flang-libraries)
484set_target_properties(flang-libraries PROPERTIES FOLDER "Misc")
485
486if (NOT LLVM_ENABLE_IDE)
487 add_llvm_install_targets(install-flang-libraries
488 DEPENDS flang-libraries
489 COMPONENT flang-libraries)
490endif()
491
492get_property(FLANG_LIBS GLOBAL PROPERTY FLANG_LIBS)
493if (FLANG_LIBS)
494 list(REMOVE_DUPLICATES FLANG_LIBS)
495 foreach(lib ${FLANG_LIBS})
496 add_dependencies(flang-libraries ${lib})
497 if (NOT LLVM_ENABLE_IDE)
498 add_dependencies(install-flang-libraries install-${lib})
499 endif()
500 endforeach()
501endif()
502
503if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Tom Eccles03f260f2024-01-16 17:04:52504 add_llvm_install_targets(install-flang-headers
505 COMPONENT flang-headers)
506
Patrick McCormick6c16aa42020-02-25 23:22:14507 install(DIRECTORY include/flang
John Ericson5c3347a2021-04-04 17:02:18508 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Patrick McCormick6c16aa42020-02-25 23:22:14509 COMPONENT flang-headers
510 FILES_MATCHING
511 PATTERN "*.def"
512 PATTERN "*.h"
513 PATTERN "*.inc"
514 PATTERN "*.td"
515 PATTERN "config.h" EXCLUDE
516 PATTERN ".git" EXCLUDE
517 PATTERN "CMakeFiles" EXCLUDE)
Stella Laurenzo38151a02022-03-04 20:08:04518
Renaud-K1e116162021-05-10 23:41:29519 install(DIRECTORY ${FLANG_INCLUDE_DIR}/flang
John Ericson5c3347a2021-04-04 17:02:18520 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Renaud-K1e116162021-05-10 23:41:29521 COMPONENT flang-headers
522 FILES_MATCHING
523 PATTERN "*.inc"
524 )
Patrick McCormick6c16aa42020-02-25 23:22:14525endif()
Pete Steinfeldddd27472023-10-30 13:44:09526
527# Put ISO_Fortran_binding.h into the include files of the build area now
528# so that we can run tests before installing
529include(GetClangResourceDir)
530get_clang_resource_dir(HEADER_BINARY_DIR PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/.. SUBDIR include)
531configure_file(
532 ${FLANG_SOURCE_DIR}/include/flang/ISO_Fortran_binding.h
533 ${HEADER_BINARY_DIR}/ISO_Fortran_binding.h)
534
535# And also install it into the install area
536get_clang_resource_dir(HEADER_INSTALL_DIR SUBDIR include)
537install(
538 FILES include/flang/ISO_Fortran_binding.h
539 DESTINATION ${HEADER_INSTALL_DIR} )