diff options
Diffstat (limited to 'rcc/cmake/modules')
| -rw-r--r-- | rcc/cmake/modules/AddOCaml.cmake | 239 | ||||
| -rw-r--r-- | rcc/cmake/modules/CheckAtomic.cmake | 125 | ||||
| -rw-r--r-- | rcc/cmake/modules/CheckCompilerVersion.cmake | 110 | ||||
| -rw-r--r-- | rcc/cmake/modules/CheckProblematicConfigurations.cmake | 19 | ||||
| -rw-r--r-- | rcc/cmake/modules/DetermineGCCCompatible.cmake | 13 | ||||
| -rw-r--r-- | rcc/cmake/modules/FindFFI.cmake | 106 | ||||
| -rw-r--r-- | rcc/cmake/modules/FindLibEdit.cmake | 65 | ||||
| -rw-r--r-- | rcc/cmake/modules/FindLibpfm.cmake | 37 | ||||
| -rw-r--r-- | rcc/cmake/modules/FindOCaml.cmake | 103 | ||||
| -rw-r--r-- | rcc/cmake/modules/GetErrcMessages.cmake | 45 | ||||
| -rw-r--r-- | rcc/cmake/modules/GetHostTriple.cmake | 98 | ||||
| -rw-r--r-- | rcc/cmake/modules/HandleRCCStdlib.cmake | 54 |
12 files changed, 1014 insertions, 0 deletions
diff --git a/rcc/cmake/modules/AddOCaml.cmake b/rcc/cmake/modules/AddOCaml.cmake new file mode 100644 index 0000000..01fb5a1 --- /dev/null +++ b/rcc/cmake/modules/AddOCaml.cmake @@ -0,0 +1,239 @@ +# CMake build rules for the OCaml language. +# Assumes FindOCaml is used. +# http://ocaml.org/ +# +# Example usage: +# +# add_ocaml_library(pkg_a OCAML mod_a OCAMLDEP pkg_b C mod_a_stubs PKG ctypes RCC core) +# +# Unnamed parameters: +# +# * Library name. +# +# Named parameters: +# +# OCAML OCaml module names. Imply presence of a corresponding .ml and .mli files. +# OCAMLDEP Names of libraries this library depends on. +# C C stub sources. Imply presence of a corresponding .c file. +# CFLAGS Additional arguments passed when compiling C stubs. +# PKG Names of ocamlfind packages this library depends on. +# RCC Names of RCC libraries this library depends on. +# NOCOPY Do not automatically copy sources (.c, .ml, .mli) from the source directory, +# e.g. if they are generated. +# + +function(add_ocaml_library name) + CMAKE_PARSE_ARGUMENTS(ARG "NOCOPY" "" "OCAML;OCAMLDEP;C;CFLAGS;PKG;RCC" ${ARGN}) + + set(src ${CMAKE_CURRENT_SOURCE_DIR}) + set(bin ${CMAKE_CURRENT_BINARY_DIR}) + + set(ocaml_pkgs) + foreach( ocaml_pkg ${ARG_PKG} ) + list(APPEND ocaml_pkgs "-package" "${ocaml_pkg}") + endforeach() + + set(sources) + + set(ocaml_inputs) + + set(ocaml_outputs "${bin}/${name}.cma") + if( ARG_C ) + list(APPEND ocaml_outputs + "${bin}/lib${name}${CMAKE_STATIC_LIBRARY_SUFFIX}") + if ( BUILD_SHARED_LIBS ) + list(APPEND ocaml_outputs + "${bin}/dll${name}${CMAKE_SHARED_LIBRARY_SUFFIX}") + endif() + endif() + if( HAVE_OCAMLOPT ) + list(APPEND ocaml_outputs + "${bin}/${name}.cmxa" + "${bin}/${name}${CMAKE_STATIC_LIBRARY_SUFFIX}") + endif() + + set(ocaml_flags "-lstdc++" "-ldopt" "-L${RCC_LIBRARY_DIR}" + "-ccopt" "-L\\$CAMLORIGIN/../.." + "-ccopt" "-Wl,-rpath,\\$CAMLORIGIN/../.." + ${ocaml_pkgs}) + + set(ocaml_dep_inputs) + foreach( ocaml_dep ${ARG_OCAMLDEP} ) + get_target_property(dep_ocaml_flags "ocaml_${ocaml_dep}" OCAML_FLAGS) + list(APPEND ocaml_flags ${dep_ocaml_flags}) + get_target_property(dep_ocaml_outputs "ocaml_${ocaml_dep}" OCAML_OUTPUTS) + list(APPEND ocaml_dep_inputs ${dep_ocaml_outputs}) + endforeach() + + if( NOT BUILD_SHARED_LIBS ) + list(APPEND ocaml_flags "-custom") + endif() + + if(RCC_LINK_RCC_DYLIB) + list(APPEND ocaml_flags "-lRCC") + else() + explicit_map_components_to_libraries(rcc_libs ${ARG_RCC}) + foreach( rcc_lib ${rcc_libs} ) + list(APPEND ocaml_flags "-l${rcc_lib}" ) + endforeach() + + get_property(system_libs TARGET RCCSupport PROPERTY RCC_SYSTEM_LIBS) + foreach(system_lib ${system_libs}) + if (system_lib MATCHES "^-") + # If it's an option, pass it without changes. + list(APPEND ocaml_flags "${system_lib}" ) + else() + # Otherwise assume it's a library name we need to link with. + list(APPEND ocaml_flags "-l${system_lib}" ) + endif() + endforeach() + endif() + + string(REPLACE ";" " " ARG_CFLAGS "${ARG_CFLAGS}") + set(c_flags "${ARG_CFLAGS} ${RCC_DEFINITIONS}") + foreach( include_dir ${RCC_INCLUDE_DIR} ${RCC_MAIN_INCLUDE_DIR} ) + set(c_flags "${c_flags} -I${include_dir}") + endforeach() + # include -D/-UNDEBUG to match dump function visibility + # regex from HandleRCCOptions.cmake + string(REGEX MATCH "(^| )[/-][UD] *NDEBUG($| )" flag_matches + "${CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${CMAKE_C_FLAGS}") + set(c_flags "${c_flags} ${flag_matches}") + + foreach( ocaml_file ${ARG_OCAML} ) + list(APPEND sources "${ocaml_file}.mli" "${ocaml_file}.ml") + + list(APPEND ocaml_inputs "${bin}/${ocaml_file}.mli" "${bin}/${ocaml_file}.ml") + + list(APPEND ocaml_outputs "${bin}/${ocaml_file}.cmi" "${bin}/${ocaml_file}.cmo") + + list(APPEND ocaml_outputs "${bin}/${ocaml_file}.cmti" "${bin}/${ocaml_file}.cmt") + + if( HAVE_OCAMLOPT ) + list(APPEND ocaml_outputs + "${bin}/${ocaml_file}.cmx" + "${bin}/${ocaml_file}${CMAKE_C_OUTPUT_EXTENSION}") + endif() + endforeach() + + foreach( c_file ${ARG_C} ) + list(APPEND sources "${c_file}.c") + + list(APPEND c_inputs "${bin}/${c_file}.c") + list(APPEND c_outputs "${bin}/${c_file}${CMAKE_C_OUTPUT_EXTENSION}") + endforeach() + + if( NOT ARG_NOCOPY ) + foreach( source ${sources} ) + add_custom_command( + OUTPUT "${bin}/${source}" + COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${src}/${source}" "${bin}" + DEPENDS "${src}/${source}" + COMMENT "Copying ${source} to build area") + endforeach() + endif() + + foreach( c_input ${c_inputs} ) + get_filename_component(basename "${c_input}" NAME_WE) + add_custom_command( + OUTPUT "${basename}${CMAKE_C_OUTPUT_EXTENSION}" + COMMAND "${OCAMLFIND}" "ocamlc" "-c" "${c_input}" -ccopt ${c_flags} + DEPENDS "${c_input}" + COMMENT "Building OCaml stub object file ${basename}${CMAKE_C_OUTPUT_EXTENSION}" + VERBATIM) + endforeach() + + set(ocaml_params) + foreach( ocaml_input ${ocaml_inputs} ${c_outputs}) + get_filename_component(filename "${ocaml_input}" NAME) + list(APPEND ocaml_params "${filename}") + endforeach() + + if( APPLE ) + set(ocaml_rpath "@executable_path/../../../lib${RCC_LIBDIR_SUFFIX}") + elseif( UNIX ) + set(ocaml_rpath "\\$ORIGIN/../../../lib${RCC_LIBDIR_SUFFIX}") + endif() + list(APPEND ocaml_flags "-ldopt" "-Wl,-rpath,${ocaml_rpath}") + + add_custom_command( + OUTPUT ${ocaml_outputs} + COMMAND "${OCAMLFIND}" "ocamlmklib" "-ocamlcflags" "-bin-annot" + "-o" "${name}" ${ocaml_flags} ${ocaml_params} + DEPENDS ${ocaml_inputs} ${ocaml_dep_inputs} ${c_outputs} + COMMENT "Building OCaml library ${name}" + VERBATIM) + + add_custom_command( + OUTPUT "${bin}/${name}.odoc" + COMMAND "${OCAMLFIND}" "ocamldoc" + "-I" "${bin}" + "-I" "${RCC_LIBRARY_DIR}/ocaml/rcc/" + "-dump" "${bin}/${name}.odoc" + ${ocaml_pkgs} ${ocaml_inputs} + DEPENDS ${ocaml_inputs} ${ocaml_dep_inputs} ${ocaml_outputs} + COMMENT "Building OCaml documentation for ${name}" + VERBATIM) + + add_custom_target("ocaml_${name}" ALL DEPENDS ${ocaml_outputs} "${bin}/${name}.odoc") + get_subproject_title(subproject_title) + set_target_properties("ocaml_${name}" PROPERTIES FOLDER "${subproject_title}/Bindings/OCaml") + + set_target_properties("ocaml_${name}" PROPERTIES + OCAML_FLAGS "-I;${bin}") + set_target_properties("ocaml_${name}" PROPERTIES + OCAML_ODOC "${bin}/${name}.odoc") + set_target_properties("ocaml_${name}" PROPERTIES + OCAML_OUTPUTS "${ocaml_outputs}") + + foreach( ocaml_dep ${ARG_OCAMLDEP} ) + add_dependencies("ocaml_${name}" "ocaml_${ocaml_dep}") + endforeach() + + if( NOT RCC_OCAML_OUT_OF_TREE ) + foreach( rcc_lib ${rcc_libs} ) + add_dependencies("ocaml_${name}" "${rcc_lib}") + endforeach() + endif() + + add_dependencies("ocaml_all" "ocaml_${name}") + + set(install_files) + set(install_shlibs) + foreach( ocaml_output ${ocaml_inputs} ${ocaml_outputs} ) + get_filename_component(ext "${ocaml_output}" EXT) + + if( NOT (ext STREQUAL ".cmo" OR + ext STREQUAL ".ml" OR + ext STREQUAL CMAKE_C_OUTPUT_EXTENSION OR + ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) ) + list(APPEND install_files "${ocaml_output}") + elseif( ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) + list(APPEND install_shlibs "${ocaml_output}") + endif() + endforeach() + + install(FILES ${install_files} + DESTINATION "${RCC_OCAML_INSTALL_PATH}/rcc") + install(FILES ${install_shlibs} + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE + GROUP_READ GROUP_EXECUTE + WORLD_READ WORLD_EXECUTE + DESTINATION "${RCC_OCAML_INSTALL_PATH}/stublibs") + + foreach( install_file ${install_files} ${install_shlibs} ) + get_filename_component(filename "${install_file}" NAME) + add_custom_command(TARGET "ocaml_${name}" POST_BUILD + COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${install_file}" + "${RCC_LIBRARY_DIR}/ocaml/rcc/" + COMMENT "Copying OCaml library component ${filename} to intermediate area" + VERBATIM) + add_dependencies("ocaml_${name}" ocaml_make_directory) + endforeach() +endfunction() + +add_custom_target(ocaml_make_directory + COMMAND "${CMAKE_COMMAND}" "-E" "make_directory" "${RCC_LIBRARY_DIR}/ocaml/rcc") +add_custom_target("ocaml_all") +set_target_properties(ocaml_all PROPERTIES FOLDER "RCC/Bindings/OCaml") +set_target_properties(ocaml_make_directory PROPERTIES FOLDER "RCC/Bindings/OCaml") diff --git a/rcc/cmake/modules/CheckAtomic.cmake b/rcc/cmake/modules/CheckAtomic.cmake new file mode 100644 index 0000000..b9ec06e --- /dev/null +++ b/rcc/cmake/modules/CheckAtomic.cmake @@ -0,0 +1,125 @@ +# atomic builtins are required for threading support. + +INCLUDE(CheckCXXSourceCompiles) +INCLUDE(CheckLibraryExists) + +# Sometimes linking against libatomic is required for atomic ops, if +# the platform doesn't support lock-free atomics. + +function(check_working_cxx_atomics varname) + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") + CHECK_CXX_SOURCE_COMPILES(" +#include <atomic> +std::atomic<int> x; +std::atomic<short> y; +std::atomic<char> z; +int main() { + ++z; + ++y; + return ++x; +} +" ${varname}) + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) +endfunction(check_working_cxx_atomics) + +function(check_working_cxx_atomics64 varname) + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}") + CHECK_CXX_SOURCE_COMPILES(" +#include <atomic> +#include <cstdint> +std::atomic<uint64_t> x (0); +int main() { + uint64_t i = x.load(std::memory_order_relaxed); + (void)i; + return 0; +} +" ${varname}) + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) +endfunction(check_working_cxx_atomics64) + + +# Check for (non-64-bit) atomic operations. +if(MSVC) + set(HAVE_CXX_ATOMICS_WITHOUT_LIB True) +elseif(RCC_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL") + # First check if atomics work without the library. + check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB) + # If not, check if the library exists, and atomics work with it. + if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB) + check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC) + if(HAVE_LIBATOMIC) + list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") + check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB) + if (NOT HAVE_CXX_ATOMICS_WITH_LIB) + message(FATAL_ERROR "Host compiler must support std::atomic!") + endif() + else() + message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.") + endif() + endif() +endif() + +# Check for 64 bit atomic operations. +if(MSVC) + set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True) +elseif(RCC_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL") + # First check if atomics work without the library. + check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB) + # If not, check if the library exists, and atomics work with it. + if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB) + check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64) + if(HAVE_CXX_LIBATOMICS64) + list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") + check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB) + if (NOT HAVE_CXX_ATOMICS64_WITH_LIB) + message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!") + endif() + else() + message(FATAL_ERROR "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.") + endif() + endif() +endif() + +# Set variable RCC_ATOMIC_LIB specifying flags for linking against libatomic. +if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB) + # Use options --push-state, --as-needed and --pop-state if linker is known to support them. + # Use single option -Wl of compiler driver to avoid incorrect re-ordering of options by CMake. + if(RCC_LINKER_IS_GNULD OR RCC_LINKER_IS_GOLD OR RCC_LINKER_IS_LLD OR RCC_LINKER_IS_MOLD) + set(RCC_ATOMIC_LIB "-Wl,--push-state,--as-needed,-latomic,--pop-state") + else() + set(RCC_ATOMIC_LIB "-latomic") + endif() +else() + set(RCC_ATOMIC_LIB) +endif() + +## TODO: This define is only used for the legacy atomic operations in +## rcc's Atomic.h, which should be replaced. Other code simply +## assumes C++11 <atomic> works. +CHECK_CXX_SOURCE_COMPILES(" +#ifdef _MSC_VER +#include <windows.h> +#endif +int main() { +#ifdef _MSC_VER + volatile LONG val = 1; + MemoryBarrier(); + InterlockedCompareExchange(&val, 0, 1); + InterlockedIncrement(&val); + InterlockedDecrement(&val); +#else + volatile unsigned long val = 1; + __sync_synchronize(); + __sync_val_compare_and_swap(&val, 1, 0); + __sync_add_and_fetch(&val, 1); + __sync_sub_and_fetch(&val, 1); +#endif + return 0; + } +" RCC_HAS_ATOMICS) + +if( NOT RCC_HAS_ATOMICS ) + message(STATUS "Warning: RCC will be built thread-unsafe because atomic builtins are missing") +endif() diff --git a/rcc/cmake/modules/CheckCompilerVersion.cmake b/rcc/cmake/modules/CheckCompilerVersion.cmake new file mode 100644 index 0000000..7a9f964 --- /dev/null +++ b/rcc/cmake/modules/CheckCompilerVersion.cmake @@ -0,0 +1,110 @@ +# Check if the host compiler is new enough. +# These versions are updated based on the following policy: +# rcc.org/docs/DeveloperPolicy.html#toolchain + +include(CheckCXXSourceCompiles) + +set(GCC_MIN 7.4) +set(GCC_SOFT_ERROR 7.4) +set(CLANG_MIN 5.0) +set(CLANG_SOFT_ERROR 5.0) +set(APPLECLANG_MIN 10.0) +set(APPLECLANG_SOFT_ERROR 10.0) + +# https://en.wikipedia.org/wiki/Microsoft_Visual_C#Internal_version_numbering +# _MSC_VER == 1928 MSVC++ 19.27 Visual Studio 2019 Version 16.8 + 16.9 +set(MSVC_MIN 19.28) +set(MSVC_SOFT_ERROR 19.28) + +set(LIBSTDCXX_MIN 7) +set(LIBSTDCXX_SOFT_ERROR 7) + + +if(DEFINED RCC_COMPILER_CHECKED) + return() +endif() +set(RCC_COMPILER_CHECKED ON) + +if(RCC_FORCE_USE_OLD_TOOLCHAIN) + return() +endif() + +function(check_compiler_version NAME NICE_NAME MINIMUM_VERSION SOFT_ERROR_VERSION) + if(NOT CMAKE_CXX_COMPILER_ID STREQUAL NAME) + return() + endif() + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS MINIMUM_VERSION) + message(FATAL_ERROR "Host ${NICE_NAME} version must be at least ${MINIMUM_VERSION}, your version is ${CMAKE_CXX_COMPILER_VERSION}.") + elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS SOFT_ERROR_VERSION) + if(RCC_TEMPORARILY_ALLOW_OLD_TOOLCHAIN) + message(WARNING "Host ${NICE_NAME} version should be at least ${SOFT_ERROR_VERSION} because RCC will soon use new C++ features which your toolchain version doesn't support. Your version is ${CMAKE_CXX_COMPILER_VERSION}. Ignoring because you've set RCC_TEMPORARILY_ALLOW_OLD_TOOLCHAIN, but very soon your toolchain won't be supported.") + else() + message(FATAL_ERROR "Host ${NICE_NAME} version should be at least ${SOFT_ERROR_VERSION} because RCC will soon use new C++ features which your toolchain version doesn't support. Your version is ${CMAKE_CXX_COMPILER_VERSION}. You can temporarily opt out using RCC_TEMPORARILY_ALLOW_OLD_TOOLCHAIN, but very soon your toolchain won't be supported.") + endif() + endif() +endfunction(check_compiler_version) + +check_compiler_version("GNU" "GCC" ${GCC_MIN} ${GCC_SOFT_ERROR}) +check_compiler_version("Clang" "Clang" ${CLANG_MIN} ${CLANG_SOFT_ERROR}) +check_compiler_version("AppleClang" "Apple Clang" ${APPLECLANG_MIN} ${APPLECLANG_SOFT_ERROR}) +check_compiler_version("MSVC" "Visual Studio" ${MSVC_MIN} ${MSVC_SOFT_ERROR}) + +# See https://developercommunity.visualstudio.com/content/problem/845933/miscompile-boolean-condition-deduced-to-be-always.html +# and thread "[rcc-dev] Longstanding failing tests - clang-tidy, MachO, Polly" +# on rcc-dev Jan 21-23 2020. +if ((${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) AND + (19.24 VERSION_LESS_EQUAL ${CMAKE_CXX_COMPILER_VERSION}) AND + (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 19.25)) + if(RCC_TEMPORARILY_ALLOW_OLD_TOOLCHAIN) + message(WARNING "Host Visual Studio version 16.4 is known to miscompile part of RCC") + else() + message(FATAL_ERROR "Host Visual Studio version 16.4 is known to miscompile part of RCC, please use clang-cl or upgrade to 16.5 or above (use -DRCC_TEMPORARILY_ALLOW_OLD_TOOLCHAIN=ON to ignore)") + endif() +endif() + + +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + if (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC") + if (CMAKE_CXX_SIMULATE_VERSION VERSION_LESS MSVC_MIN) + message(FATAL_ERROR "Host Clang must have at least -fms-compatibility-version=${MSVC_MIN}, your version is ${CMAKE_CXX_SIMULATE_VERSION}.") + endif() + set(CLANG_CL 1) + elseif(NOT RCC_ENABLE_LIBCXX) + # Test that we aren't using too old of a version of libstdc++. + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++0x") + check_cxx_source_compiles(" +#include <iosfwd> +#if defined(__GLIBCXX__) +#if !defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE < ${LIBSTDCXX_MIN} +#error Unsupported libstdc++ version +#endif +#endif +int main() { return 0; } +" + RCC_LIBSTDCXX_MIN) + if(NOT RCC_LIBSTDCXX_MIN) + message(FATAL_ERROR "libstdc++ version must be at least ${GCC_MIN}.") + endif() + check_cxx_source_compiles(" +#include <iosfwd> +#if defined(__GLIBCXX__) +#if !defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE < ${LIBSTDCXX_SOFT_ERROR} +#error Unsupported libstdc++ version +#endif +#endif +int main() { return 0; } +" + RCC_LIBSTDCXX_SOFT_ERROR) + if(NOT RCC_LIBSTDCXX_SOFT_ERROR) + if(RCC_TEMPORARILY_ALLOW_OLD_TOOLCHAIN) + message(WARNING "libstdc++ version should be at least ${LIBSTDCXX_SOFT_ERROR} because RCC will soon use new C++ features which your toolchain version doesn't support. Ignoring because you've set RCC_TEMPORARILY_ALLOW_OLD_TOOLCHAIN, but very soon your toolchain won't be supported.") + else() + message(FATAL_ERROR "libstdc++ version should be at least ${LIBSTDCXX_SOFT_ERROR} because RCC will soon use new C++ features which your toolchain version doesn't support. You can temporarily opt out using RCC_TEMPORARILY_ALLOW_OLD_TOOLCHAIN, but very soon your toolchain won't be supported.") + endif() + endif() + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES}) + endif() +endif() diff --git a/rcc/cmake/modules/CheckProblematicConfigurations.cmake b/rcc/cmake/modules/CheckProblematicConfigurations.cmake new file mode 100644 index 0000000..3b96db9 --- /dev/null +++ b/rcc/cmake/modules/CheckProblematicConfigurations.cmake @@ -0,0 +1,19 @@ + +option(RCC_ALLOW_PROBLEMATIC_CONFIGURATIONS OFF "Set this option to ON to allow problematic toolchain configurations. Use on your own risk.") + +macro(log_problematic MESSAGE) + if(RCC_ALLOW_PROBLEMATIC_CONFIGURATIONS) + message(WARNING "${MESSAGE}") + else() + message(FATAL_ERROR "${MESSAGE}\nYou can force usage of this configuration by passing -DRCC_ALLOW_PROBLEMATIC_CONFIGURATIONS=ON") + endif() +endmacro() + +# MSVC and /arch:AVX is untested and have created problems before. See: +# https://github.com/rcc/rcc-project/issues/54645 +if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + string(TOLOWER "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}" _FLAGS) + if(_FLAGS MATCHES "/arch:avx[0-9]*") + log_problematic("Compiling RCC with MSVC and the /arch:AVX flag is known to cause issues with parts of RCC.\nSee https://github.com/rcc/rcc-project/issues/54645 for details.\nUse clang-cl if you want to enable AVX instructions.") + endif() +endif() diff --git a/rcc/cmake/modules/DetermineGCCCompatible.cmake b/rcc/cmake/modules/DetermineGCCCompatible.cmake new file mode 100644 index 0000000..b645e45 --- /dev/null +++ b/rcc/cmake/modules/DetermineGCCCompatible.cmake @@ -0,0 +1,13 @@ +# Determine if the compiler has GCC-compatible command-line syntax. + +if(NOT DEFINED RCC_COMPILER_IS_GCC_COMPATIBLE) + if(CMAKE_COMPILER_IS_GNUCXX) + set(RCC_COMPILER_IS_GCC_COMPATIBLE ON) + elseif( MSVC ) + set(RCC_COMPILER_IS_GCC_COMPATIBLE OFF) + elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) + set(RCC_COMPILER_IS_GCC_COMPATIBLE ON) + elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel" ) + set(RCC_COMPILER_IS_GCC_COMPATIBLE ON) + endif() +endif() diff --git a/rcc/cmake/modules/FindFFI.cmake b/rcc/cmake/modules/FindFFI.cmake new file mode 100644 index 0000000..b1f6452 --- /dev/null +++ b/rcc/cmake/modules/FindFFI.cmake @@ -0,0 +1,106 @@ +# Attempts to discover ffi library with a linkable ffi_call function. +# +# Example usage: +# +# find_package(FFI) +# +# FFI_REQUIRE_INCLUDE may be set to consider ffi found if the includes +# are present in addition to the library. This is useful to keep off +# for the imported package on platforms that install the library but +# not the headers. +# +# FFI_LIBRARY_DIR may be set to define search paths for the ffi library. +# +# If successful, the following variables will be defined: +# FFI_FOUND +# FFI_INCLUDE_DIRS +# FFI_LIBRARIES +# FFI_STATIC_LIBRARIES +# HAVE_FFI_CALL +# +# HAVE_FFI_H or HAVE_FFI_FFI_H is defined depending on the ffi.h include path. +# +# Additionally, the following import target will be defined: +# FFI::ffi + +find_package(PkgConfig QUIET) +pkg_check_modules(PC_LIBFFI QUIET libffi) + +find_path(FFI_INCLUDE_DIRS ffi.h PATHS ${FFI_INCLUDE_DIR} ${PC_LIBFFI_INCLUDE_DIRS}) +if( EXISTS "${FFI_INCLUDE_DIRS}/ffi.h" ) + set(FFI_HEADER ffi.h CACHE INTERNAL "") + set(HAVE_FFI_H 1 CACHE INTERNAL "") +else() + find_path(FFI_INCLUDE_DIRS ffi/ffi.h PATHS ${FFI_INCLUDE_DIR}) + if( EXISTS "${FFI_INCLUDE_DIRS}/ffi/ffi.h" ) + set(FFI_HEADER ffi/ffi.h CACHE INTERNAL "") + set(HAVE_FFI_FFI_H 1 CACHE INTERNAL "") + endif() +endif() + +find_library(FFI_LIBRARIES NAMES ffi PATHS ${FFI_LIBRARY_DIR} ${PC_LIBFFI_LIBRARY_DIRS}) +find_library(FFI_STATIC_LIBRARIES NAMES libffi.a PATHS ${FFI_LIBRARY_DIR} ${PC_LIBFFI_LIBRARY_DIRS}) + +if(FFI_LIBRARIES) + include(CMakePushCheckState) + cmake_push_check_state() + list(APPEND CMAKE_REQUIRED_LIBRARIES ${FFI_LIBRARIES}) + set(HAVE_FFI_CALL_SRC [=[ + #ifdef __cplusplus + extern "C" { + #endif + struct ffi_cif; + typedef struct ffi_cif ffi_cif; + void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue); + #ifdef __cplusplus + } + #endif + int main(void) { ffi_call(0, 0, 0, 0); } + ]=]) + if(DEFINED CMAKE_C_COMPILER) + include(CheckCSourceCompiles) + check_c_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL) + else() + include(CheckCXXSourceCompiles) + check_cxx_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL) + endif() + cmake_pop_check_state() +endif() + +unset(required_includes) +if(FFI_REQUIRE_INCLUDE) + set(required_includes FFI_INCLUDE_DIRS) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(FFI + FOUND_VAR + FFI_FOUND + REQUIRED_VARS + FFI_LIBRARIES + ${required_includes} + HAVE_FFI_CALL) +mark_as_advanced(FFI_LIBRARIES + FFI_STATIC_LIBRARIES + FFI_INCLUDE_DIRS + HAVE_FFI_CALL + FFI_HEADER + HAVE_FFI_H + HAVE_FFI_FFI_H) + +if(FFI_FOUND) + if(NOT TARGET FFI::ffi AND FFI_LIBRARIES) + add_library(FFI::ffi UNKNOWN IMPORTED) + set_target_properties(FFI::ffi PROPERTIES IMPORTED_LOCATION "${FFI_LIBRARIES}") + if(FFI_INCLUDE_DIRS) + set_target_properties(FFI::ffi PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFI_INCLUDE_DIRS}") + endif() + endif() + if(NOT TARGET FFI::ffi_static AND FFI_STATIC_LIBRARIES) + add_library(FFI::ffi_static UNKNOWN IMPORTED) + set_target_properties(FFI::ffi_static PROPERTIES IMPORTED_LOCATION "${FFI_STATIC_LIBRARIES}") + if(FFI_INCLUDE_DIRS) + set_target_properties(FFI::ffi_static PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFI_INCLUDE_DIRS}") + endif() + endif() +endif() diff --git a/rcc/cmake/modules/FindLibEdit.cmake b/rcc/cmake/modules/FindLibEdit.cmake new file mode 100644 index 0000000..b3f49e1 --- /dev/null +++ b/rcc/cmake/modules/FindLibEdit.cmake @@ -0,0 +1,65 @@ +#.rst: +# FindLibEdit +# ----------- +# +# Find libedit library and headers +# +# The module defines the following variables: +# +# :: +# +# LibEdit_FOUND - true if libedit was found +# LibEdit_INCLUDE_DIRS - include search path +# LibEdit_LIBRARIES - libraries to link +# LibEdit_VERSION_STRING - version number + +find_package(PkgConfig QUIET) +pkg_check_modules(PC_LIBEDIT QUIET libedit) + +find_path(LibEdit_INCLUDE_DIRS NAMES histedit.h HINTS ${PC_LIBEDIT_INCLUDE_DIRS}) +find_library(LibEdit_LIBRARIES NAMES edit HINTS ${PC_LIBEDIT_LIBRARY_DIRS}) + +include(CheckIncludeFile) +if(LibEdit_INCLUDE_DIRS AND EXISTS "${LibEdit_INCLUDE_DIRS}/histedit.h") + include(CMakePushCheckState) + cmake_push_check_state() + list(APPEND CMAKE_REQUIRED_INCLUDES ${LibEdit_INCLUDE_DIRS}) + list(APPEND CMAKE_REQUIRED_LIBRARIES ${LibEdit_LIBRARIES}) + check_include_file(histedit.h HAVE_HISTEDIT_H) + cmake_pop_check_state() + if (HAVE_HISTEDIT_H) + file(STRINGS "${LibEdit_INCLUDE_DIRS}/histedit.h" + libedit_major_version_str + REGEX "^#define[ \t]+LIBEDIT_MAJOR[ \t]+[0-9]+") + string(REGEX REPLACE "^#define[ \t]+LIBEDIT_MAJOR[ \t]+([0-9]+)" "\\1" + libedit_major_version "${libedit_major_version_str}") + + file(STRINGS "${LibEdit_INCLUDE_DIRS}/histedit.h" + libedit_minor_version_str + REGEX "^#define[ \t]+LIBEDIT_MINOR[ \t]+[0-9]+") + string(REGEX REPLACE "^#define[ \t]+LIBEDIT_MINOR[ \t]+([0-9]+)" "\\1" + libedit_minor_version "${libedit_minor_version_str}") + + set(LibEdit_VERSION_STRING "${libedit_major_version}.${libedit_minor_version}") + else() + set(LibEdit_INCLUDE_DIRS "") + set(LibEdit_LIBRARIES "") + endif() +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LibEdit + FOUND_VAR + LibEdit_FOUND + REQUIRED_VARS + LibEdit_INCLUDE_DIRS + LibEdit_LIBRARIES + VERSION_VAR + LibEdit_VERSION_STRING) +mark_as_advanced(LibEdit_INCLUDE_DIRS LibEdit_LIBRARIES) + +if (LibEdit_FOUND AND NOT TARGET LibEdit::LibEdit) + add_library(LibEdit::LibEdit INTERFACE IMPORTED) + target_link_libraries(LibEdit::LibEdit INTERFACE ${LibEdit_LIBRARIES}) + target_include_directories(LibEdit::LibEdit INTERFACE ${LibEdit_INCLUDE_DIRS}) +endif() diff --git a/rcc/cmake/modules/FindLibpfm.cmake b/rcc/cmake/modules/FindLibpfm.cmake new file mode 100644 index 0000000..9e5900b --- /dev/null +++ b/rcc/cmake/modules/FindLibpfm.cmake @@ -0,0 +1,37 @@ +# CMake module for finding libpfm4. +# +# If successful, the following variables will be defined: +# HAVE_LIBPFM +# +# Libpfm can be disabled by setting RCC_ENABLE_LIBPFM to 0. + +include(CheckIncludeFile) +include(CheckLibraryExists) +include(CheckCXXSourceCompiles) + +if (RCC_ENABLE_LIBPFM) + check_library_exists(pfm pfm_initialize "" HAVE_LIBPFM_INITIALIZE) + if(HAVE_LIBPFM_INITIALIZE) + check_include_file(perfmon/perf_event.h HAVE_PERFMON_PERF_EVENT_H) + check_include_file(perfmon/pfmlib.h HAVE_PERFMON_PFMLIB_H) + check_include_file(perfmon/pfmlib_perf_event.h HAVE_PERFMON_PFMLIB_PERF_EVENT_H) + if(HAVE_PERFMON_PERF_EVENT_H AND HAVE_PERFMON_PFMLIB_H AND HAVE_PERFMON_PFMLIB_PERF_EVENT_H) + set(HAVE_LIBPFM 1) + # Check to see if perf_branch_entry has the field 'cycles'. + # We couldn't use CheckStructHasMember here because 'cycles' is a bit field which is not + # supported by CheckStructHasMember. + CHECK_CXX_SOURCE_COMPILES(" + #include <perfmon/perf_event.h> + int main() { + perf_branch_entry entry; + entry.cycles = 2; + return 0; + }" COMPILE_WITH_CYCLES) + if(COMPILE_WITH_CYCLES) + set(LIBPFM_HAS_FIELD_CYCLES 1) + endif() + endif() + endif() +endif() + + diff --git a/rcc/cmake/modules/FindOCaml.cmake b/rcc/cmake/modules/FindOCaml.cmake new file mode 100644 index 0000000..8eba212 --- /dev/null +++ b/rcc/cmake/modules/FindOCaml.cmake @@ -0,0 +1,103 @@ +# CMake find_package() module for the OCaml language. +# Assumes ocamlfind will be used for compilation. +# http://ocaml.org/ +# +# Example usage: +# +# find_package(OCaml) +# +# If successful, the following variables will be defined: +# OCAMLFIND +# OCAML_VERSION +# OCAML_STDLIB_PATH +# HAVE_OCAMLOPT +# +# Also provides find_ocamlfind_package() macro. +# +# Example usage: +# +# find_ocamlfind_package(ctypes) +# +# In any case, the following variables are defined: +# +# HAVE_OCAML_${pkg} +# +# If successful, the following variables will be defined: +# +# OCAML_${pkg}_VERSION + +include( FindPackageHandleStandardArgs ) + +find_program(OCAMLFIND + NAMES ocamlfind) + +if( OCAMLFIND ) + execute_process( + COMMAND ${OCAMLFIND} ocamlc -version + OUTPUT_VARIABLE OCAML_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE) + + execute_process( + COMMAND ${OCAMLFIND} ocamlc -where + OUTPUT_VARIABLE OCAML_STDLIB_PATH + OUTPUT_STRIP_TRAILING_WHITESPACE) + + execute_process( + COMMAND ${OCAMLFIND} ocamlc -version + OUTPUT_QUIET + RESULT_VARIABLE find_ocaml_result) + if( find_ocaml_result EQUAL 0 ) + set(HAVE_OCAMLOPT TRUE) + else() + set(HAVE_OCAMLOPT FALSE) + endif() +endif() + +find_package_handle_standard_args( OCaml DEFAULT_MSG + OCAMLFIND + OCAML_VERSION + OCAML_STDLIB_PATH) + +mark_as_advanced( + OCAMLFIND) + +function(find_ocamlfind_package pkg) + CMAKE_PARSE_ARGUMENTS(ARG "OPTIONAL" "VERSION" "" ${ARGN}) + + execute_process( + COMMAND "${OCAMLFIND}" "query" "${pkg}" "-format" "%v" + RESULT_VARIABLE result + OUTPUT_VARIABLE version + ERROR_VARIABLE error + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_STRIP_TRAILING_WHITESPACE) + + if( NOT result EQUAL 0 AND NOT ARG_OPTIONAL ) + message(FATAL_ERROR ${error}) + endif() + + if( result EQUAL 0 ) + set(found TRUE) + else() + set(found FALSE) + endif() + + if( found AND ARG_VERSION ) + if( version VERSION_LESS ARG_VERSION AND ARG_OPTIONAL ) + # If it's optional and the constraint is not satisfied, pretend + # it wasn't found. + set(found FALSE) + elseif( version VERSION_LESS ARG_VERSION ) + message(FATAL_ERROR + "ocamlfind package ${pkg} should have version ${ARG_VERSION} or newer") + endif() + endif() + + string(TOUPPER ${pkg} pkg) + + set(HAVE_OCAML_${pkg} ${found} + PARENT_SCOPE) + + set(OCAML_${pkg}_VERSION ${version} + PARENT_SCOPE) +endfunction() diff --git a/rcc/cmake/modules/GetErrcMessages.cmake b/rcc/cmake/modules/GetErrcMessages.cmake new file mode 100644 index 0000000..ad94f6a --- /dev/null +++ b/rcc/cmake/modules/GetErrcMessages.cmake @@ -0,0 +1,45 @@ +# This function returns the messages of various POSIX error codes as they are returned by std::error_code. +# The purpose of this function is to supply those error messages to rcc-lit using the errc_messages config. +# Currently supplied and needed error codes: ENOENT, EISDIR, EINVAL and EACCES. +# Messages are semi colon separated. +# Keep amount, order and tested error codes in sync with rcc/utils/lit/lit/rcc/config.py. +function(get_errc_messages outvar) + if(CMAKE_CROSSCOMPILING AND NOT CMAKE_CROSSCOMPILING_EMULATOR AND NOT DEFINED errc_exit_code) + set(${outvar} "" PARENT_SCOPE) + message(STATUS "Can't get errc messages in cross-compilation mode") + return() + endif() + + set(errc_test_code ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/getErrc.cpp) + + file(WRITE ${errc_test_code} " + #include <cerrno> + #include <iostream> + #include <string> + #include <system_error> + + std::string getMessageFor(int err) { + return std::make_error_code(static_cast<std::errc>(err)).message(); + } + + int main() { + std::cout << getMessageFor(ENOENT) << ';' << getMessageFor(EISDIR); + std::cout << ';' << getMessageFor(EINVAL) << ';' << getMessageFor(EACCES); + return 0; + } + ") + + try_run(errc_exit_code + errc_compiled + ${CMAKE_BINARY_DIR} + ${errc_test_code} + RUN_OUTPUT_VARIABLE errc_result + COMPILE_OUTPUT_VARIABLE errc_compile_errors) + if (errc_compiled AND "${errc_exit_code}" STREQUAL "0") + set(${outvar} ${errc_result} PARENT_SCOPE) + else() + set(${outvar} "" PARENT_SCOPE) + message(NOTICE "${errc_compile_errors}") + message(STATUS "Failed to get errc messages") + endif () +endfunction() diff --git a/rcc/cmake/modules/GetHostTriple.cmake b/rcc/cmake/modules/GetHostTriple.cmake new file mode 100644 index 0000000..046fc36 --- /dev/null +++ b/rcc/cmake/modules/GetHostTriple.cmake @@ -0,0 +1,98 @@ +# Returns the host triple. +# Invokes config.guess, except for platforms it cannot identify. + +function( get_host_triple var ) + if( MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC") + if( CMAKE_C_COMPILER_ARCHITECTURE_ID MATCHES "ARM64.*" ) + set( value "aarch64-pc-windows-msvc" ) + elseif( CMAKE_C_COMPILER_ARCHITECTURE_ID MATCHES "ARM.*" ) + set( value "armv7-pc-windows-msvc" ) + elseif( CMAKE_C_COMPILER_ARCHITECTURE_ID STREQUAL "x64" ) + set( value "x86_64-pc-windows-msvc" ) + elseif( CMAKE_C_COMPILER_ARCHITECTURE_ID STREQUAL "X86" ) + set( value "i686-pc-windows-msvc" ) + elseif( CMAKE_SIZEOF_VOID_P EQUAL 8 ) + set( value "x86_64-pc-windows-msvc" ) + else() + set( value "i686-pc-windows-msvc" ) + endif() + elseif( MINGW AND NOT MSYS ) + # CMake doesn't provide COMPILER_ARCHITECTURE_ID for Clang/GCC, + # but it does for MSVC. + if( CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "ARM.*" ) + if( CMAKE_SIZEOF_VOID_P EQUAL 8 ) + set( value "aarch64-w64-windows-gnu" ) + else() + set( value "armv7-w64-windows-gnu" ) + endif() + else() + if( CMAKE_SIZEOF_VOID_P EQUAL 8 ) + set( value "x86_64-w64-windows-gnu" ) + else() + set( value "i686-w64-windows-gnu" ) + endif() + endif() + elseif( CMAKE_SYSTEM_NAME MATCHES "OS390" ) + set( value "s390x-ibm-zos" ) + elseif( CMAKE_SYSTEM_NAME STREQUAL "AIX" ) + # We defer to dynamic detection of the host AIX version. + if( CMAKE_SIZEOF_VOID_P EQUAL 8 ) + set( value "powerpc64-ibm-aix" ) + else() + set( value "powerpc-ibm-aix" ) + endif() + elseif( CMAKE_HOST_SYSTEM_NAME MATCHES "HarmonyOS|OpenHarmony|OHOS" ) + # config.guess cannot identify HarmonyOS: uname -s reports "HarmonyOS", + # which config.guess does not recognize, and its probing is rejected by + # the system sandbox. Build the triple from CMake's host detection using + # the form clang targets on OHOS (e.g. aarch64-unknown-linux-ohos). CMake + # itself may report the host processor as "unknown" because uname -p does, + # so fall back to asking uname -m directly. + set( TT_MACHINE "${CMAKE_HOST_SYSTEM_PROCESSOR}" ) + if( NOT TT_MACHINE OR TT_MACHINE STREQUAL "unknown" ) + find_program( TT_UNAME uname PATHS /bin /usr/bin /usr/local/bin + NO_CMAKE_FIND_ROOT_PATH ) + set( TT_RV 1 ) + if( TT_UNAME ) + execute_process( COMMAND ${TT_UNAME} -m + RESULT_VARIABLE TT_RV + OUTPUT_VARIABLE TT_MACHINE + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET ) + endif() + if( NOT TT_RV EQUAL 0 ) + set( TT_MACHINE "" ) + endif() + endif() + if( TT_MACHINE MATCHES "aarch64|arm64" ) + set( value "aarch64-unknown-linux-ohos" ) + elseif( TT_MACHINE MATCHES "^arm" ) + set( value "arm-unknown-linux-ohos" ) + elseif( TT_MACHINE MATCHES "^x86_64" ) + set( value "x86_64-unknown-linux-ohos" ) + elseif( TT_MACHINE MATCHES "^riscv64" ) + set( value "riscv64-unknown-linux-ohos" ) + elseif( TT_MACHINE MATCHES "^loongarch64" ) + set( value "loongarch64-unknown-linux-ohos" ) + else() + message( FATAL_ERROR + "Failed to determine host triple for ${CMAKE_HOST_SYSTEM_NAME}: " + "host processor \"${TT_MACHINE}\" is not supported" ) + endif() + else() + if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows AND NOT MSYS) + message(WARNING "unable to determine host target triple") + else() + set(config_guess ${RCC_MAIN_SRC_DIR}/cmake/config.guess) + execute_process(COMMAND sh ${config_guess} + RESULT_VARIABLE TT_RV + OUTPUT_VARIABLE TT_OUT + OUTPUT_STRIP_TRAILING_WHITESPACE) + if( NOT TT_RV EQUAL 0 ) + message(FATAL_ERROR "Failed to execute ${config_guess}") + endif( NOT TT_RV EQUAL 0 ) + set( value ${TT_OUT} ) + endif() + endif() + set( ${var} ${value} PARENT_SCOPE ) +endfunction( get_host_triple var ) diff --git a/rcc/cmake/modules/HandleRCCStdlib.cmake b/rcc/cmake/modules/HandleRCCStdlib.cmake new file mode 100644 index 0000000..5dcaf2e --- /dev/null +++ b/rcc/cmake/modules/HandleRCCStdlib.cmake @@ -0,0 +1,54 @@ +# This CMake module is responsible for setting the standard library to libc++ +# if the user has requested it. + +include(DetermineGCCCompatible) + +if(NOT DEFINED RCC_STDLIB_HANDLED) + set(RCC_STDLIB_HANDLED ON) + + function(append value) + foreach(variable ${ARGN}) + set(${variable} "${${variable}} ${value}" PARENT_SCOPE) + endforeach(variable) + endfunction() + + include(CheckCXXCompilerFlag) + include(CheckLinkerFlag) + set(RCC_LIBCXX_USED 0) + if(RCC_ENABLE_LIBCXX) + if(RCC_COMPILER_IS_GCC_COMPATIBLE) + check_cxx_compiler_flag("-stdlib=libc++" CXX_COMPILER_SUPPORTS_STDLIB) + check_linker_flag(CXX "-stdlib=libc++" CXX_LINKER_SUPPORTS_STDLIB) + if(CXX_COMPILER_SUPPORTS_STDLIB AND CXX_LINKER_SUPPORTS_STDLIB) + append("-stdlib=libc++" + CMAKE_CXX_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS + CMAKE_MODULE_LINKER_FLAGS) + set(RCC_LIBCXX_USED 1) + else() + message(WARNING "Can't specify libc++ with '-stdlib='") + endif() + else() + message(WARNING "Not sure how to specify libc++ for this compiler") + endif() + endif() + + if(RCC_STATIC_LINK_CXX_STDLIB) + if(RCC_COMPILER_IS_GCC_COMPATIBLE) + check_cxx_compiler_flag("-static-libstdc++" + CXX_COMPILER_SUPPORTS_STATIC_STDLIB) + check_linker_flag(CXX "-static-libstdc++" CXX_LINKER_SUPPORTS_STATIC_STDLIB) + if(CXX_COMPILER_SUPPORTS_STATIC_STDLIB AND + CXX_LINKER_SUPPORTS_STATIC_STDLIB) + append("-static-libstdc++" + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS + CMAKE_MODULE_LINKER_FLAGS) + else() + message(WARNING + "Can't specify static linking for the C++ standard library") + endif() + else() + message(WARNING "Not sure how to specify static linking of C++ standard " + "library for this compiler") + endif() + endif() +endif() |
