xref: /openbsd-src/gnu/llvm/compiler-rt/cmake/Modules/CompilerRTCompile.cmake (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick# On Windows, CMAKE_*_FLAGS are built for MSVC but we use the GCC clang.exe,
23cab2bb3Spatrick# which uses completely different flags. Translate some common flag types, and
33cab2bb3Spatrick# drop the rest.
43cab2bb3Spatrickfunction(translate_msvc_cflags out_flags msvc_flags)
53cab2bb3Spatrick  # Insert an empty string in the list to simplify processing.
63cab2bb3Spatrick  set(msvc_flags ";${msvc_flags}")
73cab2bb3Spatrick
83cab2bb3Spatrick  # Canonicalize /flag to -flag.
93cab2bb3Spatrick  string(REPLACE ";/" ";-" msvc_flags "${msvc_flags}")
103cab2bb3Spatrick
113cab2bb3Spatrick  # Make space separated -D and -U flags into joined flags.
123cab2bb3Spatrick  string(REGEX REPLACE ";-\([DU]\);" ";-\\1" msvc_flags "${msvc_flags}")
133cab2bb3Spatrick
143cab2bb3Spatrick  set(clang_flags "")
153cab2bb3Spatrick  foreach(flag ${msvc_flags})
163cab2bb3Spatrick    if ("${flag}" MATCHES "^-[DU]")
173cab2bb3Spatrick      # Pass through basic command line macro definitions (-DNDEBUG).
183cab2bb3Spatrick      list(APPEND clang_flags "${flag}")
193cab2bb3Spatrick    elseif ("${flag}" MATCHES "^-O[2x]")
203cab2bb3Spatrick      # Canonicalize normal optimization flags to -O2.
213cab2bb3Spatrick      list(APPEND clang_flags "-O2")
223cab2bb3Spatrick    endif()
233cab2bb3Spatrick  endforeach()
243cab2bb3Spatrick  set(${out_flags} "${clang_flags}" PARENT_SCOPE)
253cab2bb3Spatrickendfunction()
263cab2bb3Spatrick
273cab2bb3Spatrick# Compile a sanitizer test with a freshly built clang
283cab2bb3Spatrick# for a given architecture, adding the result to the object list.
293cab2bb3Spatrick#  - obj_list: output list of objects, populated by path
303cab2bb3Spatrick#              of a generated object file.
313cab2bb3Spatrick#  - source:   source file of a test.
323cab2bb3Spatrick#  - arch:     architecture to compile for.
333cab2bb3Spatrick# sanitizer_test_compile(<obj_list> <source> <arch>
343cab2bb3Spatrick#                        KIND <custom namespace>
353cab2bb3Spatrick#                        COMPILE_DEPS <list of compile-time dependencies>
363cab2bb3Spatrick#                        DEPS <list of dependencies>
373cab2bb3Spatrick#                        CFLAGS <list of flags>
383cab2bb3Spatrick# )
393cab2bb3Spatrickfunction(sanitizer_test_compile obj_list source arch)
403cab2bb3Spatrick  cmake_parse_arguments(TEST
413cab2bb3Spatrick      "" "" "KIND;COMPILE_DEPS;DEPS;CFLAGS" ${ARGN})
423cab2bb3Spatrick  get_filename_component(basename ${source} NAME)
433cab2bb3Spatrick  set(output_obj
443cab2bb3Spatrick    "${CMAKE_CFG_RESOLVED_INTDIR}${obj_list}.${basename}.${arch}${TEST_KIND}.o")
453cab2bb3Spatrick
463cab2bb3Spatrick  # Write out architecture-specific flags into TARGET_CFLAGS variable.
473cab2bb3Spatrick  get_target_flags_for_arch(${arch} TARGET_CFLAGS)
483cab2bb3Spatrick  set(COMPILE_DEPS ${TEST_COMPILE_DEPS})
493cab2bb3Spatrick  if(NOT COMPILER_RT_STANDALONE_BUILD)
503cab2bb3Spatrick    list(APPEND COMPILE_DEPS ${TEST_DEPS})
513cab2bb3Spatrick  endif()
523cab2bb3Spatrick  clang_compile(${output_obj} ${source}
533cab2bb3Spatrick                CFLAGS ${TEST_CFLAGS} ${TARGET_CFLAGS}
543cab2bb3Spatrick                DEPS ${COMPILE_DEPS})
553cab2bb3Spatrick  list(APPEND ${obj_list} ${output_obj})
563cab2bb3Spatrick  set("${obj_list}" "${${obj_list}}" PARENT_SCOPE)
573cab2bb3Spatrickendfunction()
583cab2bb3Spatrick
593cab2bb3Spatrick# Compile a source into an object file with COMPILER_RT_TEST_COMPILER using
603cab2bb3Spatrick# a provided compile flags and dependenices.
613cab2bb3Spatrick# clang_compile(<object> <source>
623cab2bb3Spatrick#               CFLAGS <list of compile flags>
633cab2bb3Spatrick#               DEPS <list of dependencies>)
643cab2bb3Spatrickfunction(clang_compile object_file source)
653cab2bb3Spatrick  cmake_parse_arguments(SOURCE "" "" "CFLAGS;DEPS" ${ARGN})
663cab2bb3Spatrick  get_filename_component(source_rpath ${source} REALPATH)
673cab2bb3Spatrick  if(NOT COMPILER_RT_STANDALONE_BUILD)
683cab2bb3Spatrick    list(APPEND SOURCE_DEPS clang compiler-rt-headers)
693cab2bb3Spatrick  endif()
703cab2bb3Spatrick  if (TARGET CompilerRTUnitTestCheckCxx)
713cab2bb3Spatrick    list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx)
723cab2bb3Spatrick  endif()
73d89ec533Spatrick  if(COMPILER_RT_STANDALONE_BUILD)
74d89ec533Spatrick    # Only add global flags in standalone build.
753cab2bb3Spatrick    string(REGEX MATCH "[.](cc|cpp)$" is_cxx ${source_rpath})
763cab2bb3Spatrick    if(is_cxx)
773cab2bb3Spatrick      string(REPLACE " " ";" global_flags "${CMAKE_CXX_FLAGS}")
783cab2bb3Spatrick    else()
793cab2bb3Spatrick      string(REPLACE " " ";" global_flags "${CMAKE_C_FLAGS}")
803cab2bb3Spatrick    endif()
813cab2bb3Spatrick
823cab2bb3Spatrick    if (MSVC)
833cab2bb3Spatrick      translate_msvc_cflags(global_flags "${global_flags}")
843cab2bb3Spatrick    endif()
853cab2bb3Spatrick
863cab2bb3Spatrick    if (APPLE)
873cab2bb3Spatrick      set(global_flags ${OSX_SYSROOT_FLAG} ${global_flags})
883cab2bb3Spatrick    endif()
893cab2bb3Spatrick
903cab2bb3Spatrick    # Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options
913cab2bb3Spatrick    # which are not supported by Clang.
923cab2bb3Spatrick    list(APPEND global_flags -Wno-unknown-warning-option)
933cab2bb3Spatrick    set(compile_flags ${global_flags} ${SOURCE_CFLAGS})
94d89ec533Spatrick  else()
95d89ec533Spatrick    set(compile_flags ${SOURCE_CFLAGS})
96d89ec533Spatrick  endif()
97d89ec533Spatrick
98d89ec533Spatrick  string(REGEX MATCH "[.](m|mm)$" is_objc ${source_rpath})
99d89ec533Spatrick  if (is_objc)
100d89ec533Spatrick    list(APPEND compile_flags "-ObjC")
101d89ec533Spatrick  endif()
102d89ec533Spatrick
1033cab2bb3Spatrick  add_custom_command(
1043cab2bb3Spatrick    OUTPUT ${object_file}
1053cab2bb3Spatrick    COMMAND ${COMPILER_RT_TEST_COMPILER} ${compile_flags} -c
1063cab2bb3Spatrick            -o "${object_file}"
1073cab2bb3Spatrick            ${source_rpath}
1083cab2bb3Spatrick    MAIN_DEPENDENCY ${source}
109*810390e3Srobert    DEPENDS ${SOURCE_DEPS}
110*810390e3Srobert    COMMAND_EXPAND_LISTS)
1113cab2bb3Spatrickendfunction()
1123cab2bb3Spatrick
1133cab2bb3Spatrick# On Darwin, there are no system-wide C++ headers and the just-built clang is
1143cab2bb3Spatrick# therefore not able to compile C++ files unless they are copied/symlinked into
1153cab2bb3Spatrick# ${LLVM_BINARY_DIR}/include/c++
1163cab2bb3Spatrick# The just-built clang is used to build compiler-rt unit tests. Let's detect
1173cab2bb3Spatrick# this before we try to build the tests and print out a suggestion how to fix
1183cab2bb3Spatrick# it.
1193cab2bb3Spatrick# On other platforms, this is currently not an issue.
1203cab2bb3Spatrickmacro(clang_compiler_add_cxx_check)
1213cab2bb3Spatrick  if (APPLE)
1223cab2bb3Spatrick    set(CMD
1233cab2bb3Spatrick      "echo '#include <iostream>' | ${COMPILER_RT_TEST_COMPILER} ${OSX_SYSROOT_FLAG} -E -x c++ - > /dev/null"
1243cab2bb3Spatrick      "if [ $? != 0 ] "
1253cab2bb3Spatrick      "  then echo"
1263cab2bb3Spatrick      "  echo 'Your just-built clang cannot find C++ headers, which are needed to build and run compiler-rt tests.'"
1273cab2bb3Spatrick      "  echo 'You should copy or symlink your system C++ headers into ${LLVM_BINARY_DIR}/include/c++'"
1283cab2bb3Spatrick      "  if [ -d $(dirname $(dirname $(xcrun -f clang)))/include/c++ ]"
1293cab2bb3Spatrick      "    then echo 'e.g. with:'"
1303cab2bb3Spatrick      "    echo '  cp -r' $(dirname $(dirname $(xcrun -f clang)))/include/c++ '${LLVM_BINARY_DIR}/include/'"
1313cab2bb3Spatrick      "  elif [ -d $(dirname $(dirname $(xcrun -f clang)))/lib/c++ ]"
1323cab2bb3Spatrick      "    then echo 'e.g. with:'"
1333cab2bb3Spatrick      "    echo '  cp -r' $(dirname $(dirname $(xcrun -f clang)))/lib/c++ '${LLVM_BINARY_DIR}/include/'"
1343cab2bb3Spatrick      "  fi"
1353cab2bb3Spatrick      "  echo 'This can also be fixed by checking out the libcxx project from llvm.org and installing the headers'"
1363cab2bb3Spatrick      "  echo 'into your build directory:'"
1373cab2bb3Spatrick      "  echo '  cd ${LLVM_MAIN_SRC_DIR}/projects && svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx'"
1383cab2bb3Spatrick      "  echo '  cd ${LLVM_BINARY_DIR} && make -C ${LLVM_MAIN_SRC_DIR}/projects/libcxx installheaders HEADER_DIR=${LLVM_BINARY_DIR}/include'"
1393cab2bb3Spatrick      "  echo"
1403cab2bb3Spatrick      "  false"
1413cab2bb3Spatrick      "fi"
1423cab2bb3Spatrick      )
1433cab2bb3Spatrick    add_custom_target(CompilerRTUnitTestCheckCxx
1443cab2bb3Spatrick      COMMAND bash -c "${CMD}"
1453cab2bb3Spatrick      COMMENT "Checking that just-built clang can find C++ headers..."
1463cab2bb3Spatrick      VERBATIM)
147d89ec533Spatrick    if (NOT COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
1483cab2bb3Spatrick      ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang)
1493cab2bb3Spatrick    endif()
1503cab2bb3Spatrick  endif()
1513cab2bb3Spatrickendmacro()
152