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>
include(cmake/version.cmake)
include(GNUInstallDirs)
include(CheckCXXCompilerFlag)
+include(cmake/FindCompilerFlag.cmake)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
--- /dev/null
+# 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()
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()