2021-04-04 13:02:18 -04:00
|
|
|
include(GNUInstallDirs)
|
2020-10-01 14:35:28 -07:00
|
|
|
include(LLVMDistributionSupport)
|
|
|
|
|
2020-02-25 16:22:14 -07:00
|
|
|
macro(set_flang_windows_version_resource_properties name)
|
|
|
|
if (DEFINED windows_resource_file)
|
|
|
|
set_windows_version_resource_properties(${name} ${windows_resource_file}
|
|
|
|
VERSION_MAJOR ${FLANG_VERSION_MAJOR}
|
|
|
|
VERSION_MINOR ${FLANG_VERSION_MINOR}
|
|
|
|
VERSION_PATCHLEVEL ${FLANG_VERSION_PATCHLEVEL}
|
2022-10-25 21:57:19 -07:00
|
|
|
VERSION_STRING "${FLANG_VERSION}"
|
2020-02-25 16:22:14 -07:00
|
|
|
PRODUCT_NAME "flang")
|
|
|
|
endif()
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(add_flang_subdirectory name)
|
|
|
|
add_llvm_subdirectory(FLANG TOOL ${name})
|
|
|
|
endmacro()
|
|
|
|
|
2023-04-16 14:19:46 +00:00
|
|
|
function(add_flang_library name)
|
2023-10-02 09:01:11 +00:00
|
|
|
set(options SHARED STATIC INSTALL_WITH_TOOLCHAIN)
|
2023-04-16 14:19:46 +00:00
|
|
|
set(multiValueArgs ADDITIONAL_HEADERS CLANG_LIBS)
|
2020-02-25 16:22:14 -07:00
|
|
|
cmake_parse_arguments(ARG
|
2023-04-16 14:19:46 +00:00
|
|
|
"${options}"
|
2020-02-25 16:22:14 -07:00
|
|
|
""
|
2023-04-16 14:19:46 +00:00
|
|
|
"${multiValueArgs}"
|
2020-02-25 16:22:14 -07:00
|
|
|
${ARGN})
|
2023-04-16 14:19:46 +00:00
|
|
|
|
2020-02-25 16:22:14 -07:00
|
|
|
set(srcs)
|
|
|
|
if (MSVC_IDE OR XCODE)
|
|
|
|
# Add public headers
|
|
|
|
file(RELATIVE_PATH lib_path
|
|
|
|
${FLANG_SOURCE_DIR}/lib/
|
|
|
|
${CMAKE_CURRENT_SOURCE_DIR})
|
|
|
|
if(NOT lib_path MATCHES "^[.][.]")
|
|
|
|
file( GLOB_RECURSE headers
|
|
|
|
${FLANG_SOURCE_DIR}/include/flang/${lib_path}/*.h
|
|
|
|
${FLANG_SOURCE_DIR}/include/flang/${lib_path}/*.def)
|
|
|
|
set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
|
|
|
|
|
|
|
|
if (headers)
|
|
|
|
set(srcs ${headers})
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endif(MSVC_IDE OR XCODE)
|
|
|
|
|
|
|
|
if (srcs OR ARG_ADDITIONAL_HEADERS)
|
|
|
|
set(srcs
|
|
|
|
ADDITIONAL_HEADERS
|
|
|
|
${srcs}
|
|
|
|
${ARG_ADDITIONAL_HEADERS}) # It may contain unparsed unknown args.
|
|
|
|
|
|
|
|
endif()
|
|
|
|
|
Avoid object libraries in the VS IDE (#93519)
As discussed in #89743, when using the Visual Studio solution
generators, object library projects are displayed as a collection of
non-editable *.obj files. To look for the corresponding source files,
one has to browse (or search) to the library's obj.libname project. This
patch tries to avoid this as much as possible.
For Clang, there is already an exception for XCode. We handle MSVC_IDE
the same way.
For MLIR, this is more complicated. There are explicit references to the
obj.libname target that only work when there is an object library. This
patch cleans up the reasons for why an object library is needed:
1. The obj.libname is modified in the calling CMakeLists.txt. Note that
with use-only references, `add_library(<name> ALIAS <target>)` could
have been used.
2. An `libMLIR.so` (mlir-shlib) is also created. This works by adding
linking the object libraries' object files into the libMLIR.so (in
addition to the library's own .so/.a). XCode is handled using the
`-force_load` linker option instead. Windows is not supported. This
mechanism is different from LLVM's llvm-shlib that is created by linking
static libraries with `-Wl,--whole-archive` (and `-Wl,-all_load` on
MacOS).
3. The library might be added to an aggregate library. In-tree, the
seems to be only `libMLIR-C.so` and the standalone example. In XCode, it
uses the object library and `-force_load` mechanism as above. Again,
this is different from `libLLVM-C.so`.
4. Build an object library whenever it was before this patch, except
when generating a Visual Studio solution. This condition could be
removed, but I am trying to avoid build breakages of whatever
configurations others use.
This seems to never have worked with XCode because of the explicit
references to obj.libname (reason 1.). I don't have access to XCode, but
I tried to preserve the current working. IMHO there should be a common
mechanism to build aggregate libraries for all LLVM projects instead of
the 4 that we have now.
As far as I can see, this means for LLVM there are the following changes
on whether object libraries are created:
1. An object library is created even in XCode if FORCE_OBJECT_LIBRARY is
set. I do not know how XCode handles it, but I also know CMake will
abort otherwise.
2. An object library is created even for explicitly SHARED libraries for
building `libMLIR.so`. Again, mlir-shlib does not work otherwise.
`libMLIR.so` itself is created using SHARED so this patch is marking it
as EXCLUDE_FROM_LIBMLIR.
3. For the second condition, it is now sensitive to whether the
mlir-shlib is built at all (LLVM_BUILD_LLVM_DYLIB). However, an object
library is still built using the fourth condition unless using the MSVC
solution generator. That is, except with MSVC_IDE, when an object
library was built before, it will also be an object library now.
2024-06-19 14:30:01 +02:00
|
|
|
if(ARG_SHARED AND ARG_STATIC)
|
|
|
|
set(LIBTYPE SHARED STATIC)
|
|
|
|
elseif(ARG_SHARED)
|
2020-02-25 16:22:14 -07:00
|
|
|
set(LIBTYPE SHARED)
|
[flang] Simplify LIBTYPE logic (#98072)
When the `add_flang_library` was first added, it was apparently copied
over from `add_clang_library`, including its logic to determine the
library type. It includes a workaround: If `BUILD_SHARED_LIBS` is
enabled, it should build all libraries as shared, including those that
are explicitly marked as `STATIC`[^1], because `add_clang_library`
always passes at least one of `STATIC`/`SHARED` to `llvm_add_library`,
and `llvm_add_library` could not distinguish the two cases.
Then, the two implementations diverged. For its runtime libraries, Flang
requires some libraries to always be static libraries, so if a library
is explicitly marked as `STATIC`, `BUILD_SHARED_LIBS` is ignored[^2].
I noticed the two implementations of the same functionality, modified
only the `add_clang_library`, and copied over the result to
`add_flang_library`[^3], without noticing that they are slightly
different. As a result, Flang runtime libraries would be built as shared
libraries with `-DBUILD_SHARED_LIBS=ON`, which may break some build
configurations[^4].
This PR fixes the problem and at the same time simplifies the library
type algorithm by just passing SHARED/STATIC verbatim to
`llvm_add_library`. This is effectively what [^2] should have done
instead adding more code to undo the workaround of [^1]. Ideally, one
would use
```
llvm_add_library(${name} ${ARG_STATIC} ${ARG_SHARED} [...])
```
but `ARG_STATIC`/`ARG_SHARED` as set by `cmake_parse_arguments` contain
`TRUE`/`FALSE` instead of the keywords themselves. I could imagine a
utility function akin to `pythonize_bool` that does this.
This simplification adds two more changes:
1. Object libraries are not explicitly requested anymore.
`llvm_add_library` itself should determine whether an object library is
necessary. As the comment notes, using an object library is not without
problems and seem of no use here since it works fine without object
library when in `XCODE`/`MSVC_IDE` mode.
2. The property `CLANG_STATIC_LIBS` was removed. It was
`FLANG_STATIC_LIBS` before to copy&paste error of #93519 [^3] which not
used anywhere. In clang, `CLANG_STATIC_LIBS` is used for `clang-shlib`
to include all component libraries in a single large library. There is
no equivalent `flang-shlib`.
[^1]: dbc2a12c7311ff4cc2cd7887d128b506bd35b579
[^2]: 3d2e05d542e646891745c5278a09950d3c4fb4a5
[^3]: #93519
[^4]:
https://github.com/llvm/llvm-project/pull/93519#issuecomment-2192359002
2024-07-12 16:11:55 +02:00
|
|
|
elseif(ARG_STATIC)
|
|
|
|
# If BUILD_SHARED_LIBS and ARG_STATIC are both set, llvm_add_library prioritizes STATIC.
|
|
|
|
# This is required behavior for libFortranFloat128Math.
|
|
|
|
set(LIBTYPE STATIC)
|
2020-02-25 16:22:14 -07:00
|
|
|
else()
|
[flang] Simplify LIBTYPE logic (#98072)
When the `add_flang_library` was first added, it was apparently copied
over from `add_clang_library`, including its logic to determine the
library type. It includes a workaround: If `BUILD_SHARED_LIBS` is
enabled, it should build all libraries as shared, including those that
are explicitly marked as `STATIC`[^1], because `add_clang_library`
always passes at least one of `STATIC`/`SHARED` to `llvm_add_library`,
and `llvm_add_library` could not distinguish the two cases.
Then, the two implementations diverged. For its runtime libraries, Flang
requires some libraries to always be static libraries, so if a library
is explicitly marked as `STATIC`, `BUILD_SHARED_LIBS` is ignored[^2].
I noticed the two implementations of the same functionality, modified
only the `add_clang_library`, and copied over the result to
`add_flang_library`[^3], without noticing that they are slightly
different. As a result, Flang runtime libraries would be built as shared
libraries with `-DBUILD_SHARED_LIBS=ON`, which may break some build
configurations[^4].
This PR fixes the problem and at the same time simplifies the library
type algorithm by just passing SHARED/STATIC verbatim to
`llvm_add_library`. This is effectively what [^2] should have done
instead adding more code to undo the workaround of [^1]. Ideally, one
would use
```
llvm_add_library(${name} ${ARG_STATIC} ${ARG_SHARED} [...])
```
but `ARG_STATIC`/`ARG_SHARED` as set by `cmake_parse_arguments` contain
`TRUE`/`FALSE` instead of the keywords themselves. I could imagine a
utility function akin to `pythonize_bool` that does this.
This simplification adds two more changes:
1. Object libraries are not explicitly requested anymore.
`llvm_add_library` itself should determine whether an object library is
necessary. As the comment notes, using an object library is not without
problems and seem of no use here since it works fine without object
library when in `XCODE`/`MSVC_IDE` mode.
2. The property `CLANG_STATIC_LIBS` was removed. It was
`FLANG_STATIC_LIBS` before to copy&paste error of #93519 [^3] which not
used anywhere. In clang, `CLANG_STATIC_LIBS` is used for `clang-shlib`
to include all component libraries in a single large library. There is
no equivalent `flang-shlib`.
[^1]: dbc2a12c7311ff4cc2cd7887d128b506bd35b579
[^2]: 3d2e05d542e646891745c5278a09950d3c4fb4a5
[^3]: #93519
[^4]:
https://github.com/llvm/llvm-project/pull/93519#issuecomment-2192359002
2024-07-12 16:11:55 +02:00
|
|
|
# Let llvm_add_library decide, taking BUILD_SHARED_LIBS into account.
|
|
|
|
set(LIBTYPE)
|
2020-02-25 16:22:14 -07:00
|
|
|
endif()
|
|
|
|
llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
|
|
|
|
|
2023-04-16 14:19:46 +00:00
|
|
|
clang_target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS})
|
|
|
|
|
2020-02-25 16:22:14 -07:00
|
|
|
if (TARGET ${name})
|
|
|
|
|
2022-08-11 11:21:40 +02:00
|
|
|
if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libflang"
|
|
|
|
OR ARG_INSTALL_WITH_TOOLCHAIN)
|
2020-10-01 14:35:28 -07:00
|
|
|
get_target_export_arg(${name} Flang export_to_flangtargets UMBRELLA flang-libraries)
|
2020-02-25 16:22:14 -07:00
|
|
|
install(TARGETS ${name}
|
|
|
|
COMPONENT ${name}
|
|
|
|
${export_to_flangtargets}
|
2022-08-18 22:44:46 -04:00
|
|
|
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
|
|
|
|
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
|
2021-04-04 13:02:18 -04:00
|
|
|
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
2020-02-25 16:22:14 -07:00
|
|
|
|
|
|
|
if (NOT LLVM_ENABLE_IDE)
|
|
|
|
add_llvm_install_targets(install-${name}
|
|
|
|
DEPENDS ${name}
|
|
|
|
COMPONENT ${name})
|
|
|
|
endif()
|
|
|
|
|
|
|
|
set_property(GLOBAL APPEND PROPERTY FLANG_LIBS ${name})
|
|
|
|
endif()
|
|
|
|
set_property(GLOBAL APPEND PROPERTY FLANG_EXPORTS ${name})
|
|
|
|
else()
|
|
|
|
# Add empty "phony" target
|
|
|
|
add_custom_target(${name})
|
|
|
|
endif()
|
|
|
|
|
2024-05-25 17:24:58 +02:00
|
|
|
set_target_properties(${name} PROPERTIES FOLDER "Flang/Libraries")
|
2020-02-25 16:22:14 -07:00
|
|
|
set_flang_windows_version_resource_properties(${name})
|
2023-04-16 14:19:46 +00:00
|
|
|
endfunction(add_flang_library)
|
2020-02-25 16:22:14 -07:00
|
|
|
|
|
|
|
macro(add_flang_executable name)
|
|
|
|
add_llvm_executable(${name} ${ARGN})
|
|
|
|
set_flang_windows_version_resource_properties(${name})
|
|
|
|
endmacro(add_flang_executable)
|
|
|
|
|
|
|
|
macro(add_flang_tool name)
|
|
|
|
if (NOT FLANG_BUILD_TOOLS)
|
|
|
|
set(EXCLUDE_FROM_ALL ON)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
add_flang_executable(${name} ${ARGN})
|
|
|
|
|
|
|
|
if (FLANG_BUILD_TOOLS)
|
2020-10-01 14:35:28 -07:00
|
|
|
get_target_export_arg(${name} Flang export_to_flangtargets)
|
2020-02-25 16:22:14 -07:00
|
|
|
install(TARGETS ${name}
|
|
|
|
${export_to_flangtargets}
|
2021-04-04 13:02:18 -04:00
|
|
|
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
|
2020-02-25 16:22:14 -07:00
|
|
|
COMPONENT ${name})
|
|
|
|
|
|
|
|
if(NOT LLVM_ENABLE_IDE)
|
|
|
|
add_llvm_install_targets(install-${name}
|
|
|
|
DEPENDS ${name}
|
|
|
|
COMPONENT ${name})
|
|
|
|
endif()
|
|
|
|
set_property(GLOBAL APPEND PROPERTY FLANG_EXPORTS ${name})
|
|
|
|
endif()
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(add_flang_symlink name dest)
|
[cmake] Don't export `LLVM_TOOLS_INSTALL_DIR` anymore
First of all, `LLVM_TOOLS_INSTALL_DIR` put there breaks our NixOS
builds, because `LLVM_TOOLS_INSTALL_DIR` defined the same as
`CMAKE_INSTALL_BINDIR` becomes an *absolute* path, and then when
downstream projects try to install there too this breaks because our
builds always install to fresh directories for isolation's sake.
Second of all, note that `LLVM_TOOLS_INSTALL_DIR` stands out against the
other specially crafted `LLVM_CONFIG_*` variables substituted in
`llvm/cmake/modules/LLVMConfig.cmake.in`.
@beanz added it in d0e1c2a550ef348aae036d0fe78cab6f038c420c to fix a
dangling reference in `AddLLVM`, but I am suspicious of how this
variable doesn't follow the pattern.
Those other ones are carefully made to be build-time vs install-time
variables depending on which `LLVMConfig.cmake` is being generated, are
carefully made relative as appropriate, etc. etc. For my NixOS use-case
they are also fine because they are never used as downstream install
variables, only for reading not writing.
To avoid the problems I face, and restore symmetry, I deleted the
exported and arranged to have many `${project}_TOOLS_INSTALL_DIR`s.
`AddLLVM` now instead expects each project to define its own, and they
do so based on `CMAKE_INSTALL_BINDIR`. `LLVMConfig` still exports
`LLVM_TOOLS_BINARY_DIR` which is the location for the tools defined in
the usual way, matching the other remaining exported variables.
For the `AddLLVM` changes, I tried to copy the existing pattern of
internal vs non-internal or for LLVM vs for downstream function/macro
names, but it would good to confirm I did that correctly.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D117977
2022-06-11 06:11:59 +00:00
|
|
|
llvm_add_tool_symlink(FLANG ${name} ${dest} ALWAYS_GENERATE)
|
2020-02-25 16:22:14 -07:00
|
|
|
# Always generate install targets
|
[cmake] Don't export `LLVM_TOOLS_INSTALL_DIR` anymore
First of all, `LLVM_TOOLS_INSTALL_DIR` put there breaks our NixOS
builds, because `LLVM_TOOLS_INSTALL_DIR` defined the same as
`CMAKE_INSTALL_BINDIR` becomes an *absolute* path, and then when
downstream projects try to install there too this breaks because our
builds always install to fresh directories for isolation's sake.
Second of all, note that `LLVM_TOOLS_INSTALL_DIR` stands out against the
other specially crafted `LLVM_CONFIG_*` variables substituted in
`llvm/cmake/modules/LLVMConfig.cmake.in`.
@beanz added it in d0e1c2a550ef348aae036d0fe78cab6f038c420c to fix a
dangling reference in `AddLLVM`, but I am suspicious of how this
variable doesn't follow the pattern.
Those other ones are carefully made to be build-time vs install-time
variables depending on which `LLVMConfig.cmake` is being generated, are
carefully made relative as appropriate, etc. etc. For my NixOS use-case
they are also fine because they are never used as downstream install
variables, only for reading not writing.
To avoid the problems I face, and restore symmetry, I deleted the
exported and arranged to have many `${project}_TOOLS_INSTALL_DIR`s.
`AddLLVM` now instead expects each project to define its own, and they
do so based on `CMAKE_INSTALL_BINDIR`. `LLVMConfig` still exports
`LLVM_TOOLS_BINARY_DIR` which is the location for the tools defined in
the usual way, matching the other remaining exported variables.
For the `AddLLVM` changes, I tried to copy the existing pattern of
internal vs non-internal or for LLVM vs for downstream function/macro
names, but it would good to confirm I did that correctly.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D117977
2022-06-11 06:11:59 +00:00
|
|
|
llvm_install_symlink(FLANG ${name} ${dest} ALWAYS_GENERATE)
|
2020-02-25 16:22:14 -07:00
|
|
|
endmacro()
|