build: use CMake to drive the position independence
authorSaleem Abdulrasool <compnerd@compnerd.org>
Sun, 25 Apr 2021 00:50:08 +0000 (17:50 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 1 May 2021 04:36:27 +0000 (21:36 -0700)
This replaces the use of `-fPIC` with `CMAKE_POSITION_INDEPENDENT_CODE`
which is used by CMake to initialise the `POSITION_INDEPENDENT_CODE`
property on targets, which allows the portable way to indicate the
compiler-independent spelling for `-fPIC`.  This was previously unset on
MSVC because MSVC does not support the `-fPIC` option and Windows by
default is PIC by means of the base relocation of the binary.

CMakeLists.txt

index e1c91108d188a11f6c15da2f4cd465cb9a1ed092..a11b1744e767d210dd2092a7f3e38d18c64852b8 100644 (file)
@@ -181,6 +181,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
 set(CMAKE_CXX_STANDARD 11)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
+set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+
 if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
     # MSVC warning C4996 mostly fires on completely valid code. The changes
     # proposed in the warning text often seriously compromise the code
@@ -189,8 +191,6 @@ if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996")
 else()
-    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
-    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
     # Enable common warnings flags
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")