authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2023-03-08 14:21:33+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-08 17:47:36-05:00
log3e99afdbfe9a66bc5461148a4754c1e88b33fb88
tree580d0b66d24850d82ae4780ef1559c505dd306bc
parent06b263825a67e68cec128c640a6287fa1716dc63

build: add -Dpie option

It is becoming increasingly common for distributions to want to enable PIE for all binaries and zig currently does not provide any way to do so aside from patching the build.zig.

2 files changed, 9 insertions(+), 0 deletions(-)

CMakeLists.txt+7
...@@ -827,6 +827,12 @@ else()...@@ -827,6 +827,12 @@ else()
827 set(ZIG_STATIC_ARG "")827 set(ZIG_STATIC_ARG "")
828endif()828endif()
829829
830if(CMAKE_POSITION_INDEPENDENT_CODE)
831 set(ZIG_PIE_ARG="-Dpie")
832else()
833 set(ZIG_PIE_ARG="")
834endif()
835
830set(ZIG_BUILD_ARGS836set(ZIG_BUILD_ARGS
831 --zig-lib-dir "${CMAKE_SOURCE_DIR}/lib"837 --zig-lib-dir "${CMAKE_SOURCE_DIR}/lib"
832 "-Dconfig_h=${ZIG_CONFIG_H_OUT}"838 "-Dconfig_h=${ZIG_CONFIG_H_OUT}"
...@@ -835,6 +841,7 @@ set(ZIG_BUILD_ARGS...@@ -835,6 +841,7 @@ set(ZIG_BUILD_ARGS
835 ${ZIG_STATIC_ARG}841 ${ZIG_STATIC_ARG}
836 ${ZIG_NO_LIB_ARG}842 ${ZIG_NO_LIB_ARG}
837 ${ZIG_SINGLE_THREADED_ARG}843 ${ZIG_SINGLE_THREADED_ARG}
844 ${ZIG_PIE_ARG}
838 "-Dtarget=${ZIG_TARGET_TRIPLE}"845 "-Dtarget=${ZIG_TARGET_TRIPLE}"
839 "-Dcpu=${ZIG_TARGET_MCPU}"846 "-Dcpu=${ZIG_TARGET_MCPU}"
840 "-Dversion-string=${RESOLVED_ZIG_VERSION}"847 "-Dversion-string=${RESOLVED_ZIG_VERSION}"
build.zig+2
...@@ -152,6 +152,7 @@ pub fn build(b: *std.Build) !void {...@@ -152,6 +152,7 @@ pub fn build(b: *std.Build) !void {
152 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or only_c);152 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or only_c);
153 const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;153 const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;
154 const strip = b.option(bool, "strip", "Omit debug information");154 const strip = b.option(bool, "strip", "Omit debug information");
155 const pie = b.option(bool, "pie", "Produce a Position Independent Executable");
155 const value_tracing = b.option(bool, "value-tracing", "Enable extra state tracking to help troubleshoot bugs in the compiler (using the std.debug.Trace API)") orelse false;156 const value_tracing = b.option(bool, "value-tracing", "Enable extra state tracking to help troubleshoot bugs in the compiler (using the std.debug.Trace API)") orelse false;
156157
157 const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: {158 const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: {
...@@ -162,6 +163,7 @@ pub fn build(b: *std.Build) !void {...@@ -162,6 +163,7 @@ pub fn build(b: *std.Build) !void {
162163
163 const exe = addCompilerStep(b, optimize, target);164 const exe = addCompilerStep(b, optimize, target);
164 exe.strip = strip;165 exe.strip = strip;
166 exe.pie = pie;
165 exe.sanitize_thread = sanitize_thread;167 exe.sanitize_thread = sanitize_thread;
166 exe.build_id = b.option(bool, "build-id", "Include a build id note") orelse false;168 exe.build_id = b.option(bool, "build-id", "Include a build id note") orelse false;
167 exe.install();169 exe.install();