Skip to content

Commit

Permalink
Handling position independent code
Browse files Browse the repository at this point in the history
  • Loading branch information
dutow committed Mar 16, 2018
1 parent f2abe7d commit e888477
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 1 deletion.
9 changes: 8 additions & 1 deletion PrecompiledHeader.cmake
Expand Up @@ -64,6 +64,13 @@ macro(combine_arguments _variable)
endmacro()

function(export_all_flags _filename mode)
get_target_property(_target_type ${_target} TYPE)
if(_target_type STREQUAL "EXECUTABLE")
set(_pic "$<$<BOOL:$<TARGET_PROPERTY:${_target},POSITION_INDEPENDENT_CODE>>:-fPIE\n>")
else()
set(_pic "$<$<BOOL:$<TARGET_PROPERTY:${_target},POSITION_INDEPENDENT_CODE>>:-fPIC\n>")
endif()

set(_include_directories "$<TARGET_PROPERTY:${_target},INCLUDE_DIRECTORIES>")
set(_compile_definitions "$<TARGET_PROPERTY:${_target},COMPILE_DEFINITIONS>")
set(_compile_flags "$<TARGET_PROPERTY:${_target},COMPILE_FLAGS>")
Expand Down Expand Up @@ -100,7 +107,7 @@ function(export_all_flags _filename mode)
endif()
set(_standard_check "${_standard_check}\n")

file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}${_standard_check}\n")
file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}${_standard_check}${_pic}\n")
endfunction()

function (gcc_pch_targets_for_mode _input _pch_path mode)
Expand Down
16 changes: 16 additions & 0 deletions test/cxx-pic-shared/CMakeLists.txt
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.3)
cmake_policy(SET CMP0058 NEW)
enable_testing()
project(test-cxx-pic-shared)
include(../../PrecompiledHeader.cmake)
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_library(test-cxx-pic-lib SHARED test-cxx-lib.cpp test-pch.h ${pch_source})
add_precompiled_header(test-cxx-pic-lib test-pch.h FORCEINCLUDE)
add_executable(test-cxx-pic-shared test-cxx.cpp)
target_link_libraries(test-cxx-pic-shared test-cxx-pic-lib)
add_test(test-cxx-pic-shared test-cxx-pic-shared)
4 changes: 4 additions & 0 deletions test/cxx-pic-shared/test-cxx-lib.cpp
@@ -0,0 +1,4 @@
#ifndef PCH
#error Missing precompiled header
#endif
int foo() { return 1; }
5 changes: 5 additions & 0 deletions test/cxx-pic-shared/test-cxx.cpp
@@ -0,0 +1,5 @@
#include "test-pch.h"
#ifndef PCH
#error Missing precompiled header
#endif
int main() { return foo() == 1 && !(PCH == atoi(getenv("EXPECTED_PCH"))); }
1 change: 1 addition & 0 deletions test/cxx-pic-shared/test-pch.cpp
@@ -0,0 +1 @@
#include "test-pch.h"
7 changes: 7 additions & 0 deletions test/cxx-pic-shared/test-pch.h
@@ -0,0 +1,7 @@
#define PCH 1
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
int foo();
17 changes: 17 additions & 0 deletions test/cxx-pic/CMakeLists.txt
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.3)
cmake_policy(SET CMP0058 NEW)
enable_testing()
project(test-cxx-pic)
include(../../PrecompiledHeader.cmake)
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_library(test-cxx-pic-lib STATIC test-cxx-lib.cpp test-pch.h ${pch_source})
set_target_properties(test-cxx-pic-lib PROPERTIES POSITION_INDEPENDENT_CODE ON)
add_precompiled_header(test-cxx-pic-lib test-pch.h FORCEINCLUDE)
add_executable(test-cxx-pic test-cxx.cpp)
target_link_libraries(test-cxx-pic test-cxx-pic-lib)
add_test(test-cxx-pic test-cxx-pic)
4 changes: 4 additions & 0 deletions test/cxx-pic/test-cxx-lib.cpp
@@ -0,0 +1,4 @@
#ifndef PCH
#error Missing precompiled header
#endif
int foo() { return 1; }
5 changes: 5 additions & 0 deletions test/cxx-pic/test-cxx.cpp
@@ -0,0 +1,5 @@
#include "test-pch.h"
#ifndef PCH
#error Missing precompiled header
#endif
int main() { return foo() == 1 && !(PCH == atoi(getenv("EXPECTED_PCH"))); }
1 change: 1 addition & 0 deletions test/cxx-pic/test-pch.cpp
@@ -0,0 +1 @@
#include "test-pch.h"
7 changes: 7 additions & 0 deletions test/cxx-pic/test-pch.h
@@ -0,0 +1,7 @@
#define PCH 1
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
int foo();
15 changes: 15 additions & 0 deletions test/cxx-pie/CMakeLists.txt
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.3)
cmake_policy(SET CMP0058 NEW)
enable_testing()
project(test-cxx-pie)
include(../../PrecompiledHeader.cmake)
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_executable(test-cxx-pie test-cxx.cpp test-pch.h ${pch_source})
set_target_properties(test-cxx-pie PROPERTIES POSITION_INDEPENDENT_CODE ON)
add_precompiled_header(test-cxx-pie test-pch.h FORCEINCLUDE)
add_test(test-cxx-pie test-cxx-pie)
4 changes: 4 additions & 0 deletions test/cxx-pie/test-cxx.cpp
@@ -0,0 +1,4 @@
#ifndef PCH
#error Missing precompiled header
#endif
int main() { return !(PCH == atoi(getenv("EXPECTED_PCH"))); }
1 change: 1 addition & 0 deletions test/cxx-pie/test-pch.cpp
@@ -0,0 +1 @@
#include "test-pch.h"
6 changes: 6 additions & 0 deletions test/cxx-pie/test-pch.h
@@ -0,0 +1,6 @@
#define PCH 1
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

0 comments on commit e888477

Please sign in to comment.