Skip to content

Commit

Permalink
πŸ› Fix cross-compilation setup for C/C++
Browse files Browse the repository at this point in the history
- Use tools from `pkgsBuildBuild` where appropriate.
  Many of these tools do not actually care about targetPlatform
  so getting them from pkgsBuildBuild makes sure that we do
  not need to rebuild them when cross compiling.

- Fix doxygen output requiring `name`. `pname` is now also
  correctly accepted.
  • Loading branch information
abbec committed Dec 18, 2024
1 parent d5af691 commit 2fc98cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## Fixed
- c/c++: Use more appropriate versions of clangd and clang-format when
cross-compiling since the target platform does not actually
matter.
- c/c++: Use a debugger for the shell that comes from buildPackages
to get the correct hostPlatform when cross-compiling.
- c/c++: Enabling doxygen required the `name` attribute. Now,
pname is enough as well.
- c/c++: Use a more appropriate variant of `fd`, since it
does not depend on the target platform when cross-compiling.

## [5.1.0] - 2024-12-12

### Added
Expand Down
39 changes: 26 additions & 13 deletions c/make-derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ platformOverrides:
, pkgs
, lib
, buildPackages
, pkgsBuildBuild
, substitute
, writeShellScriptBin
, components
, targetName
, doxygenOutputDir ? "doc"
Expand All @@ -29,7 +29,7 @@ let
replacements = [
"--subst-var-by"
"name"
attrs.name
attrs.name or attrs.pname
"--subst-var-by"
"version"
attrs.version
Expand All @@ -40,15 +40,15 @@ let
};


wrappedDoxygen = outputDir: doxyfiles: writeShellScriptBin "doxygen" ''
wrappedDoxygen = outputDir: doxyfiles: pkgsBuildBuild.writeShellScriptBin "doxygen" ''
set -euo pipefail
doxyfiles=(${builtins.concatStringsSep " " doxyfiles})
: ''${componentDir=$PWD}
if [ -e "$componentDir/Doxyfile" ]; then
doxyfiles+=("$componentDir/Doxyfile")
fi
if [[ $@ =~ "--print-generated-config" ]]; then
${buildPackages.bat}/bin/bat "''${doxyfiles[@]}"
${pkgsBuildBuild.bat}/bin/bat "''${doxyfiles[@]}"
exit 0
fi
Expand All @@ -57,9 +57,9 @@ let
cp --no-preserve=mode "${mathjax}" "${outputDir}"/html/mathjax/MathJax.js
if [ $# -eq 0 ]; then
${buildPackages.doxygen}/bin/doxygen <(cat "''${doxyfiles[@]}")
${pkgsBuildBuild.doxygen}/bin/doxygen <(cat "''${doxyfiles[@]}")
else
${buildPackages.doxygen}/bin/doxygen "$@"
${pkgsBuildBuild.doxygen}/bin/doxygen "$@"
fi
'';

Expand All @@ -85,7 +85,7 @@ let
outputs = [ "out" ] ++ lib.optionals (attrs.enableDoxygen or enableDoxygen) [ "doc" "man" ];
} // attrs // {
nativeBuildInputs = [
buildPackages.clang-tools
pkgsBuildBuild.clang-tools
buildPackages.valgrind
]
++ attrs.nativeBuildInputs or [ ]
Expand All @@ -98,15 +98,25 @@ let
]
);

shellInputs = [
pkgs.${attrs.debugger or "gdb"}
] ++ attrs.shellInputs or [ ];
shellInputs = [ buildPackages.${attrs.debugger or "gdb"} ]
++ attrs.shellInputs or [ ];

lintPhase = attrs.lintPhase or ''
runHook preLint
sourcePath="."
if [ -n "''${componentDir:-}" ]; then
sourcePath="$componentDir"
elif [ -n "''${sourceRoot:-}" ]; then
sourcePath="''${NIX_BUILD_TOP:-.}/$sourceRoot"
fi
if [ -z "''${dontCheckClangFormat:-}" ]; then
echo "🏟 Checking format in C/C++ files..."
${buildPackages.fd}/bin/fd --ignore-file=.gitignore --glob '*.{h,hpp,hh,cpp,cxx,cc,c}' --exec-batch clang-format -Werror -n --style=LLVM
echo "🏟 Checking format in C/C++ files at \"$sourcePath\"..."
${pkgsBuildBuild.fd}/bin/fd \
--ignore-file=.gitignore \
--glob '*.{h,hpp,hh,cpp,cxx,cc,c}' \
--exec-batch clang-format --verbose -Werror -n --style=LLVM \; \
"$sourcePath"
rc=$?
if [ $rc -eq 0 ]; then
Expand All @@ -129,7 +139,10 @@ let
script = ''
runHook preFormat
echo "🏟️ Formatting C++ files..."
${buildPackages.fd}/bin/fd --glob '*.{h,hpp,hh,cpp,cxx,cc,c}' --exec-batch clang-format --style=LLVM -i "$@"
${pkgsBuildBuild.fd}/bin/fd \
--glob '*.{h,hpp,hh,cpp,cxx,cc,c}' \
--exec-batch clang-format --style=LLVM -i "$@" \; \
"''${componentDir:-.}"
runHook postFormat
'';
description = "Format source code in the component.";
Expand Down

0 comments on commit 2fc98cc

Please sign in to comment.