lnstools

Linux namespace tools
git clone https://ccx.te2000.cz/git/lnstools
Log | Files | Refs

commit c59d074a64255216715a01921f2644ad59fb43fe
parent 685ae1e301d8d4901b9ed0ead7f72f7dfcbb1343
Author: Jan Pobrislo <ccx@te2000.cz>
Date:   Sun, 16 Nov 2025 12:45:26 +0000

Create simplistic configure script to generate compiler and linker scripts

Diffstat:
MMakefile | 7+++++--
Aconfigure | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/gen-cc | 37+++++++++++++++++++++++++++++++++++++
Ascripts/gen-ld | 28++++++++++++++++++++++++++++
Msimplelink.mk | 20++++++++++++--------
5 files changed, 136 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,9 +1,12 @@ -all: all_executables -.PHONY: all +default: static_executables +.PHONY: default executables:=lns-lockdown lns-envuidgid lns-applyuidgid lns-pidns include simplelink.mk +build/cc build/ld.shared build/ld.static: + @printf '%s\n' "Please run configure script first!"; exit 1 + clean: rm -r build .PHONY: clean diff --git a/configure b/configure @@ -0,0 +1,54 @@ +#!/bin/sh +cd "$(dirname "$0")" || exit $? +sh=$(which sh) || exit $? +export sh + +# CC +# CFLAGS +: ${CFLAGS=-D_GNU_SOURCE -Werror -pipe -std=c11 -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -ffunction-sections -fdata-sections -g} +export CFLAGS +# CPATH +# CPPFLAGS +# C_INCLUDE_PATH +# LDFLAGS +# LDLIBS +: ${LDLIBS=-lskarnet -lcap} +export LDLIBS +# LIBRARY_PATH +# LOADLIBES + +replace_if_different() { + if ! test -e "$1"; then + mv "$1.new" "$1" || exit $? + return 0 + fi + local R + cmp -s "$1" "$1.new" + R=$? + case $R in + (0) + rm "$1.new" || exit $? + ;; + (1) + mv "$1.new" "$1" || exit $? + ;; + (*) + printf >&2 "configure: fatal: failed to compare files %s and %s\n" "$1" "$1.new" + exit $R + ;; + esac +} + +generate() { + local out=$1 + shift || exit $? + "$@" >"$out.new" || exit $? + chmod +x "$out.new" || exit $? + replace_if_different "$out" || exit $? +} + +mkdir -p build || exit $? + +generate build/cc awk -f ./scripts/gen-cc +generate build/ld.shared awk -f ./scripts/gen-ld +generate build/ld.static env LDFLAGS="$LDFLAGS -static" awk -f ./scripts/gen-ld diff --git a/scripts/gen-cc b/scripts/gen-cc @@ -0,0 +1,37 @@ +#!/bin/awk -f +#LIBRARY_PATH +#CPATH +#C_INCLUDE_PATH +#CPLUS_INCLUDE_PATH +#OBJC_INCLUDE_PATH +# '$(CC) $(CPPFLAGS) $(CFLAGS) -c' +# '$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c' +# '$(CC) $(LDFLAGS) N.o $(LOADLIBES) $(LDLIBS)' + +function shquote(text) { + if(text !~ "[^-+,_./:@0-9A-Za-z]") { + return text + } + gsub("'", "'\\''", text) + return "'" text "'" +} + +function or_(first, second) { + return length(first) ? first : second +} + +function set_env(var) { + if(var in ENVIRON) { + print var "=" shquote(ENVIRON[var]) + } else { + print "unset -v " var + } +} + +BEGIN { + print "#!" or_(ENVIRON["sh"], "/bin/sh") + set_env("LIBRARY_PATH") + set_env("CPATH") + set_env("C_INCLUDE_PATH") + print "exec " or_(ENVIRON["CC"], "gcc") " " ENVIRON["CPPFLAGS"] " " ENVIRON["CFLAGS"] " \"$@\"" +} diff --git a/scripts/gen-ld b/scripts/gen-ld @@ -0,0 +1,28 @@ +#!/bin/awk -f + +function shquote(text) { + if(text !~ "[^-+,_./:@0-9A-Za-z]") { + return text + } + gsub("'", "'\\''", text) + return "'" text "'" +} + +function or_(first, second) { + return length(first) ? first : second +} + +function set_env(var) { + if(var in ENVIRON) { + print var "=" shquote(ENVIRON[var]) + } else { + print "unset -v " var + } +} + +BEGIN { + print "#!" or_(ENVIRON["sh"], "/bin/sh") + set_env("LIBRARY_PATH") + print "exec " or_(ENVIRON["CC"], "gcc") " " ENVIRON["LDFLAGS"] " \"$@\" " ENVIRON["LOADLIBES"] " " ENVIRON["LDLIBS"] +} + diff --git a/simplelink.mk b/simplelink.mk @@ -2,25 +2,29 @@ SRC_DIR ?= src BUILD_DIR ?= build SCRIPTS_DIR ?= scripts -all_executables: $(patsubst %,$(BUILD_DIR)/%,$(executables)) -.PHONY: all_executables +shared_executables: $(patsubst %,$(BUILD_DIR)/bin-shared/%,$(executables)) +static_executables: $(patsubst %,$(BUILD_DIR)/bin-static/%,$(executables)) +.PHONY: shared_executables static_executables define simplelink = include $$(BUILD_DIR)/$(1)_main.c.deps.mk -$$(BUILD_DIR)/$(1): $$(LINKDEP_$(subst -,__,$(1))_main__c) $$(SCRIPTS_DIR)/link $$(BUILD_DIR)/$(1)_main.c.deps.mk - $$(SCRIPTS_DIR)/link -o '$$@' $$(LINKDEP_$(subst -,__,$(1))_main__c) +$$(BUILD_DIR)/bin-shared/$(1): $$(LINKDEP_$(subst -,__,$(1))_main__c) $$(BUILD_DIR)/ld.shared $$(BUILD_DIR)/$(1)_main.c.deps.mk $$(BUILD_DIR)/bin-shared/.exists + $$(BUILD_DIR)/ld.shared -o '$$@' $$(LINKDEP_$(subst -,__,$(1))_main__c) +include $$(BUILD_DIR)/$(1)_main.c.deps.mk +$$(BUILD_DIR)/bin-static/$(1): $$(LINKDEP_$(subst -,__,$(1))_main__c) $$(BUILD_DIR)/ld.static $$(BUILD_DIR)/$(1)_main.c.deps.mk $$(BUILD_DIR)/bin-static/.exists + $$(BUILD_DIR)/ld.static -o '$$@' $$(LINKDEP_$(subst -,__,$(1))_main__c) endef $(foreach var,$(executables),$(eval $(call simplelink,$(var)))) ## pattern rules: # preprocess C sources -$(BUILD_DIR)/%.c.i: $(SRC_DIR)/%.c $(SCRIPTS_DIR)/cc $(BUILD_DIR)/.exists - $(SCRIPTS_DIR)/cc -E -C -o '$@' '$(SRC_DIR)/$*.c' +$(BUILD_DIR)/%.c.i: $(SRC_DIR)/%.c $(BUILD_DIR)/cc $(BUILD_DIR)/.exists + $(BUILD_DIR)/cc -E -C -o '$@' '$(SRC_DIR)/$*.c' # compile preprocessed C sources -$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c.i $(SCRIPTS_DIR)/cc - $(SCRIPTS_DIR)/cc -fpreprocessed -c -o '$@' '$(BUILD_DIR)/$*.c.i' +$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c.i $(BUILD_DIR)/cc + $(BUILD_DIR)/cc -fpreprocessed -c -o '$@' '$(BUILD_DIR)/$*.c.i' # extract dependencies from preprocessed sources $(BUILD_DIR)/%.c.deps.mk: $(BUILD_DIR)/%.c.i $(SCRIPTS_DIR)/codedeps.awk