From bd140bb156892894b1d5af52540cce4ba411f65c Mon Sep 17 00:00:00 2001 From: user Date: Mon, 23 Dec 2024 04:24:03 +0300 Subject: [PATCH] Package with Nix, add build-flags wscript parameter --- build.nix | 19 +++++++++ flake.lock | 26 ++++++++++++ flake.nix | 16 +++++++ hl2-unwrapped.nix | 48 +++++++++++++++++++++ hl2-wrapper.nix | 105 ++++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 23 ++++++++++ wscript | 10 +++++ 7 files changed, 247 insertions(+) create mode 100644 build.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 hl2-unwrapped.nix create mode 100644 hl2-wrapper.nix create mode 100644 shell.nix diff --git a/build.nix b/build.nix new file mode 100644 index 00000000..c4058c1c --- /dev/null +++ b/build.nix @@ -0,0 +1,19 @@ +{ callPackage +, symlinkJoin +}: + +let + # Strangely, these are overridable + hl2-unwrapped = callPackage ./hl2-unwrapped.nix { }; + hl2-wrapper = callPackage ./hl2-wrapper.nix { inherit hl2-unwrapped; }; +in +symlinkJoin { + name = "hl2"; + + paths = [ + hl2-unwrapped + hl2-wrapper + ]; + + postBuild = "ln -s $out/bin/hl2-wrapper $out/bin/hl2"; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..cf9e0127 --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1734566935, + "narHash": "sha256-cnBItmSwoH132tH3D4jxmMLVmk8G5VJ6q/SC3kszv9E=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "087408a407440892c1b00d80360fd64639b8091d", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..eb722573 --- /dev/null +++ b/flake.nix @@ -0,0 +1,16 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs"; + }; + + outputs = { self, nixpkgs }: + let + pkgs = nixpkgs.legacyPackages.x86_64-linux; + in + { + packages.x86_64-linux = { + hl2 = pkgs.callPackage ./build.nix { }; + default = self.packages.x86_64-linux.hl2; + }; + }; +} diff --git a/hl2-unwrapped.nix b/hl2-unwrapped.nix new file mode 100644 index 00000000..1a2cfef6 --- /dev/null +++ b/hl2-unwrapped.nix @@ -0,0 +1,48 @@ +{ stdenv +, python3 +, wafHook +, callPackage +, SDL2 +, freetype +, fontconfig +, zlib +, bzip2 +, libjpeg +, libpng +, curl +, openal +, libopus +, pkg-config +, gcc +, extraWafConfigureFlags ? [ ] +}: + +stdenv.mkDerivation { + pname = "hl2"; + version = "1.0"; + + src = ./.; + + nativeBuildInputs = [ + python3 + SDL2 + freetype + fontconfig + zlib + bzip2 + libjpeg + libpng + curl + openal + libopus + pkg-config + gcc + wafHook + ]; + + wafConfigureFlags = [ + "-T fastnative" + "--disable-warns" + "--build-flags=-Wno-error=format-security,-O3" + ] ++ extraWafConfigureFlags; +} diff --git a/hl2-wrapper.nix b/hl2-wrapper.nix new file mode 100644 index 00000000..1151bdd9 --- /dev/null +++ b/hl2-wrapper.nix @@ -0,0 +1,105 @@ +{ writeShellScriptBin +, getopt +, xorg +, hl2-unwrapped +}: + +writeShellScriptBin "hl2-wrapper" '' + config_path=$HOME/.config/hl2/config + default_resource_path=$HOME/hl2 + + longoptions="resource-path:,impermanent,help" + shortoptions="r:h" + + impermanent=false + + usage() { + echo "hl2(-wrapper) usage:" + echo "Define resource_path variable in $config_path, with the argument shown below or put resource files in $default_resource_path." + grep ' \+-.*) ' $0 | sed 's/#//' | sed -r 's/([a-z])\)/\1/' + exit 0 + } + + # Just in case + save_newly_created_files() { + newly_created_files=$(cd $tmp_dir && find . -type f | cut -c 2-) + + echo "$newly_created_files" | while read file; + do + cp $tmp_dir$file $resource_path$file + done + } + + cleanup() { + if ! $impermanent; then + save_newly_created_files + fi + + rm -rf $tmp_dir + } + + if [[ -e $config_path ]] + then + source $config_path + fi + + parsed=$(${getopt}/bin/getopt -l $longoptions -o $shortoptions -a -- "$@") + + eval set -- "$parsed" + + while true; do + case "$1" in + -r | --resource-path) # Defines resources folder path + resource_path="$2" + shift 2 + ;; + --impermanent) # Don't save newly created files that are not in symlimked folders (you probably don't want to use this) + impermanent=true + shift 1 + ;; + -h | --help) # Get help + usage + exit 0 + ;; + --) + shift + break + ;; + *) + echo "Wrong argument: $1" + usage + exit 1 + ;; + esac + done + + if [[ ! $resource_path ]] + then + echo "The path to Half-Life 2 resource folder is not set either in a config file or with an argument. Looking for resource files in the standard directory (~/hl2)." + resource_path=$default_resource_path + fi + + if [[ ! -d $resource_path ]] + then + echo "$resource_path doesn't exist. Set a proper resource path." + exit 1 + elif [[ ! -d $resource_path/hl2 || ! -d $resource_path/platform ]] + then + echo "$resource_path doesn't contain 'hl2' and/or 'platform' folder. Set a proper resource path." + exit 1 + fi + + tmp_dir=$(mktemp -d) + echo $tmp_dir + + mkdir $tmp_dir/{hl2,platform} + ln -s $resource_path/hl2/* $tmp_dir/hl2/ + ln -s $resource_path/platform/* $tmp_dir/platform/ + rm -rf $tmp_dir/hl2/bin + + ${xorg.lndir}/bin/lndir "${hl2-unwrapped}" $tmp_dir + + trap cleanup EXIT + + ( cd $tmp_dir ; ./hl2_launcher ) +'' diff --git a/shell.nix b/shell.nix new file mode 100644 index 00000000..4422a6c3 --- /dev/null +++ b/shell.nix @@ -0,0 +1,23 @@ +{ pkgs ? import {} }: +pkgs.mkShell rec { + nativeBuildInputs = with pkgs; [ + makeWrapper + SDL2 + freetype + fontconfig + zlib + bzip2 + libjpeg + libpng + curl + openal + libopus + pkg-config + gcc + ]; + buildInputs = [ + (pkgs.python3.withPackages (ps: with ps; [ + ipython + ])) + ]; +} diff --git a/wscript b/wscript index 16f2263b..a179fbf9 100644 --- a/wscript +++ b/wscript @@ -322,6 +322,9 @@ def options(opt): grp.add_option('--sanitize', action = 'store', dest = 'SANITIZE', default = '', help = 'build with sanitizers [default: %default]') + grp.add_option('--build-flags', action = 'store', dest = 'BUILD_FLAGS', type = 'string', + help = 'specify build flags (both cflags and cxxflags) separated by a comma. Note that this does NOT override flags produced automatically by waf!') + opt.load('compiler_optimizations subproject') opt.load('xcompile compiler_cxx compiler_c sdl2 clang_compilation_database strip_on_install_v2 waf_unit_test subproject') @@ -598,6 +601,13 @@ def configure(conf): cxxflags += conf.filter_cxxflags(compiler_optional_flags, cflags) cflags += conf.filter_cflags(compiler_optional_flags + c_compiler_optional_flags, cflags) + preferred_flags = conf.options.BUILD_FLAGS + if preferred_flags: + preferred_flags = preferred_flags.split(',') + cflags += preferred_flags + cxxflags += preferred_flags + linkflags += preferred_flags + conf.env.append_unique('CFLAGS', cflags) conf.env.append_unique('CXXFLAGS', cxxflags) conf.env.append_unique('LINKFLAGS', linkflags)