fix lua-bcc build issue with cmake try_compile
authorYonghong Song <yhs@fb.com>
Wed, 23 Aug 2017 20:40:47 +0000 (13:40 -0700)
committerBrenden Blanco <bblanco@gmail.com>
Thu, 24 Aug 2017 01:17:31 +0000 (18:17 -0700)
Certain versions of recent gcc (e.g., gcc 6.3.0 on ubuntu17.04)
has pie enabled by default at linker (collect2) stage.
The compilation flag "-no-pie" is available to negate
this linker option.

Add -no-pie to compilation flag only if it is available.
Earlier gcc compiler may not have this option but it does
not have linker pie on-by-default either.

Tested with ubuntu 17.04 and my local gcc 4.8.5 (not accepting -no-pie).

Signed-off-by: Yonghong Song <yhs@fb.com>
CMakeLists.txt
cmake/FindCompilerFlag.cmake [new file with mode: 0644]
src/lua/CMakeLists.txt

index 858e12a..b23261c 100644 (file)
@@ -13,6 +13,7 @@ include(cmake/GetGitRevisionDescription.cmake)
 include(cmake/version.cmake)
 include(GNUInstallDirs)
 include(CheckCXXCompilerFlag)
+include(cmake/FindCompilerFlag.cmake)
 
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
 
diff --git a/cmake/FindCompilerFlag.cmake b/cmake/FindCompilerFlag.cmake
new file mode 100644 (file)
index 0000000..8a6fbd4
--- /dev/null
@@ -0,0 +1,15 @@
+# Copyright (c) 2017 Facebook, Inc.
+# Licensed under the Apache License, Version 2.0 (the "License")
+
+if (CMAKE_C_COMPILER_ID MATCHES "Clang")
+       set(COMPILER_NOPIE_FLAG "-nopie")
+else()
+       set(CMAKE_REQUIRED_FLAGS "-no-pie")
+       CHECK_CXX_SOURCE_COMPILES("int main() {return 0;}"
+                                 HAVE_NO_PIE_FLAG)
+       if (HAVE_NO_PIE_FLAG)
+               set(COMPILER_NOPIE_FLAG "-no-pie")
+       else()
+               set(COMPILER_NOPIE_FLAG "")
+       endif()
+endif()
index 09e456d..076706b 100644 (file)
@@ -24,11 +24,9 @@ if (LUAJIT_LIBRARIES AND LUAJIT)
        set_target_properties(bcc-lua PROPERTIES LINKER_LANGUAGE C)
        target_link_libraries(bcc-lua ${LUAJIT_LIBRARIES})
        target_link_libraries(bcc-lua -Wl,--whole-archive bcc-static -Wl,--no-whole-archive)
-  if (CMAKE_C_COMPILER_ID MATCHES "Clang")
-    target_link_libraries(bcc-lua -nopie)
-  else()
-    target_link_libraries(bcc-lua --no-pie)
-  endif()
+       if (NOT COMPILER_NOPIE_FLAG EQUAL "")
+               target_link_libraries(bcc-lua ${COMPILER_NOPIE_FLAG})
+       endif()
 
        install(TARGETS bcc-lua RUNTIME DESTINATION bin)
 endif()