Skip to content

Commit 654ac5f

Browse files
authored
Use cmake POSITION_INDEPENDENT_CODE instead of hardcoding -pie -fPIE (#1598)
This fixes unused option warnings on -pie for macOS. (On macOS cmake produces "-fPIE -Xlinker -pie") Bump required cmake version to 3.14 for CheckPIESupported. References: https://cmake.org/cmake/help/latest/prop_tgt/POSITION_INDEPENDENT_CODE.html https://cmake.org/cmake/help/latest/module/CheckPIESupported.html#module:CheckPIESupported
1 parent e87a554 commit 654ac5f

File tree

12 files changed

+68
-26
lines changed

12 files changed

+68
-26
lines changed

CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
107107

108108
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
109109

110-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE")
111-
112110
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow")
113111
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
114112

product-mini/platforms/linux/CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 2.9)
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
57

68
project (iwasm)
79

@@ -119,7 +121,7 @@ set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
119121
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
120122
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
121123

122-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE")
124+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
123125

124126
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow")
125127
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
@@ -164,6 +166,9 @@ include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
164166

165167
add_executable (iwasm main.c ${UNCOMMON_SHARED_SOURCE})
166168

169+
check_pie_supported()
170+
set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
171+
167172
install (TARGETS iwasm DESTINATION bin)
168173

169174
target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)

samples/basic/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 2.9)
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
57

68
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
79
project (basic)
@@ -56,7 +58,6 @@ endif ()
5658

5759
if (NOT MSVC)
5860
# linker flags
59-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
6061
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
6162
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
6263
endif ()
@@ -80,6 +81,9 @@ include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
8081

8182
add_executable (basic src/main.c src/native_impl.c ${UNCOMMON_SHARED_SOURCE})
8283

84+
check_pie_supported()
85+
set_target_properties (basic PROPERTIES POSITION_INDEPENDENT_CODE ON)
86+
8387
if (APPLE)
8488
target_link_libraries (basic vmlib -lm -ldl -lpthread)
8589
else ()

samples/file/src/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (C) 2022 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 3.0)
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
57

68
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
79
project (iwasm)
@@ -56,7 +58,6 @@ endif ()
5658

5759
if (NOT MSVC)
5860
# linker flags
59-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
6061
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
6162
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
6263
endif ()
@@ -80,6 +81,9 @@ include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
8081

8182
add_executable (iwasm main.c ${UNCOMMON_SHARED_SOURCE})
8283

84+
check_pie_supported()
85+
set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
86+
8387
if (APPLE)
8488
target_link_libraries (iwasm vmlib -lm -ldl -lpthread)
8589
else ()

samples/multi-module/CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 2.8...3.16)
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
58
project(multi_module)
69

710
################ runtime settings ################
@@ -47,7 +50,6 @@ set(WAMR_BUILD_LIBC_WASI 1)
4750
set(WAMR_BUILD_MULTI_MODULE 1)
4851

4952
# compiling and linking flags
50-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
5153
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
5254
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
5355
endif ()
@@ -150,5 +152,8 @@ add_executable(multi_module src/main.c ${UNCOMMON_SHARED_SOURCE})
150152

151153
add_dependencies(multi_module vmlib WASM_MODULE)
152154

155+
check_pie_supported()
156+
set_target_properties (multi_module PROPERTIES POSITION_INDEPENDENT_CODE ON)
157+
153158
# libraries
154159
target_link_libraries(multi_module PRIVATE vmlib -lpthread -lm)

samples/multi-thread/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required(VERSION 2.8)
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
58
project(pthread)
69

710
################ runtime settings ################
@@ -48,7 +51,6 @@ set(WAMR_BUILD_LIB_PTHREAD 1)
4851
set(WAMR_BUILD_LIB_PTHREAD_SEMAPHORE 1)
4952

5053
# compiling and linking flags
51-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
5254
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
5355
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
5456
endif ()
@@ -73,5 +75,7 @@ set (RUNTIME_SOURCE_ALL
7375
${UNCOMMON_SHARED_SOURCE}
7476
)
7577
add_executable (iwasm ${RUNTIME_SOURCE_ALL})
78+
check_pie_supported()
79+
set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
7680
target_link_libraries(iwasm vmlib -lpthread -lm -ldl)
7781

samples/native-lib/CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required(VERSION 3.0)
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
58
project(native_lib)
69

710
################ runtime settings ##############
@@ -46,7 +49,6 @@ set (WAMR_BUILD_LIBC_BUILTIN 1)
4649
set (WAMR_BUILD_FAST_INTERP 1)
4750

4851
# compiling and linking flags
49-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
5052
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
5153
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
5254
endif ()
@@ -68,6 +70,9 @@ set (RUNTIME_SOURCE_ALL
6870

6971
add_executable (iwasm ${RUNTIME_SOURCE_ALL})
7072

73+
check_pie_supported()
74+
set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
75+
7176
target_link_libraries(iwasm vmlib -lpthread -lm -ldl)
7277

7378
################ native libraries ###############

samples/ref-types/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 2.9)
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
57

68
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
79
project(ref-types)
@@ -70,7 +72,6 @@ set(WAMR_BUILD_REF_TYPES 1)
7072

7173
if (NOT MSVC)
7274
# compiling and linking flags
73-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
7475
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
7576
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
7677
endif ()
@@ -107,6 +108,9 @@ include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
107108

108109
add_executable(hello src/hello.c ${UNCOMMON_SHARED_SOURCE})
109110

111+
check_pie_supported()
112+
set_target_properties (hello PROPERTIES POSITION_INDEPENDENT_CODE ON)
113+
110114
target_include_directories(hello PRIVATE ${UNCOMMON_SHARED_DIR})
111115

112116
target_link_libraries(hello vmlib -lpthread -lm)

samples/socket-api/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required(VERSION 2.8...3.18)
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
58
project(socket_api_sample)
69

710
#######################################
@@ -170,7 +173,6 @@ set(WAMR_BUILD_LIBC_WASI 1)
170173
set(WAMR_BUILD_LIB_PTHREAD 1)
171174

172175
# compiling and linking flags
173-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
174176
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
175177
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
176178
endif ()
@@ -188,4 +190,6 @@ set (RUNTIME_SOURCE_ALL
188190
${UNCOMMON_SHARED_SOURCE}
189191
)
190192
add_executable (iwasm ${RUNTIME_SOURCE_ALL})
193+
check_pie_supported()
194+
set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
191195
target_link_libraries(iwasm vmlib -lpthread -lm -ldl)

samples/spawn-thread/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required(VERSION 2.8)
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
58
project(spawn_thread)
69

710
################ runtime settings ################
@@ -47,7 +50,6 @@ set(WAMR_BUILD_FAST_INTERP 1)
4750
set(WAMR_BUILD_LIB_PTHREAD 1)
4851

4952
# compiling and linking flags
50-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
5153
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
5254
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
5355
endif ()
@@ -72,4 +74,6 @@ set (RUNTIME_SOURCE_ALL
7274
${UNCOMMON_SHARED_SOURCE}
7375
)
7476
add_executable (spawn_thread ${RUNTIME_SOURCE_ALL})
77+
check_pie_supported()
78+
set_target_properties (spawn_thread PROPERTIES POSITION_INDEPENDENT_CODE ON)
7579
target_link_libraries(spawn_thread vmlib -lpthread -lm)

samples/wasm-c-api/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 2.9)
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
57

68
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
79
project(c-api)
@@ -71,7 +73,6 @@ endif()
7173

7274
if (NOT MSVC)
7375
# compiling and linking flags
74-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
7576
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
7677
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
7778
endif ()
@@ -142,10 +143,13 @@ if(WAMR_BUILD_JIT AND WAMR_BUILD_LAZY_JIT)
142143
endif()
143144
endif()
144145

146+
check_pie_supported()
147+
145148
foreach(EX ${EXAMPLES})
146149
set(SRC ${CMAKE_CURRENT_LIST_DIR}/src/${EX}.c)
147150

148151
add_executable(${EX} ${SRC} ${UNCOMMON_SHARED_SOURCE} ${MM_UTIL})
152+
set_target_properties (${EX} PROPERTIES POSITION_INDEPENDENT_CODE ON)
149153
target_include_directories(${EX} PRIVATE ${UNCOMMON_SHARED_DIR})
150154
target_link_libraries(${EX} vmlib -lpthread -lm)
151155
if (MSVC)

wamr-compiler/CMakeLists.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 2.9)
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
57

68
if (NOT DEFINED WAMR_BUILD_PLATFORM)
79
if (CMAKE_SYSTEM_NAME)
@@ -237,9 +239,6 @@ endif()
237239
if (NOT MSVC)
238240
add_definitions(-D_FORTIFY_SOURCE=2)
239241
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftrapv")
240-
if (NOT WIN32)
241-
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pie -fPIE")
242-
endif()
243242
endif()
244243

245244
if (WIN32)
@@ -264,6 +263,8 @@ add_library (vmlib
264263
add_library (aotclib ${IWASM_COMPL_SOURCE})
265264

266265
add_executable (wamrc main.c)
266+
check_pie_supported()
267+
set_target_properties (wamrc PROPERTIES POSITION_INDEPENDENT_CODE ON)
267268

268269
if (LLVM_LINK_LLVM_DYLIB)
269270
set(WAMRC_LINK_LLVM_LIBS LLVM)

0 commit comments

Comments
 (0)