From 3409eb3c115023406173149e631ce2d5025f0c68 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 22 Jul 2020 06:43:56 +0800 Subject: [PATCH] conda-windows-binutils-or1k: build with msys2, not conda --- .../conda-windows/binutils-recipe/bld.bat | 22 ++------- artiq-fast/conda-windows/binutils.nix | 10 +++-- artiq-fast/conda-windows/patch_prefix.py | 35 +++++++++++++++ artiq-fast/default.nix | 8 +++- .../binutils-hack-libiconv.patch | 0 artiq-fast/windows/binutils.nix | 45 +++++++++++++++++++ 6 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 artiq-fast/conda-windows/patch_prefix.py rename artiq-fast/{conda-windows => windows}/binutils-hack-libiconv.patch (100%) create mode 100644 artiq-fast/windows/binutils.nix diff --git a/artiq-fast/conda-windows/binutils-recipe/bld.bat b/artiq-fast/conda-windows/binutils-recipe/bld.bat index fa5fe84..90e4ff0 100644 --- a/artiq-fast/conda-windows/binutils-recipe/bld.bat +++ b/artiq-fast/conda-windows/binutils-recipe/bld.bat @@ -1,21 +1,7 @@ set MSYS=C:\MSYS64 -set TRIPLE=x86_64-pc-mingw64 set PATH=%MSYS%\usr\bin;%MSYS%\mingw64\bin;%PATH% -mkdir build -cd build -set CFLAGS=-I%PREFIX:\=/%/Library/include/ -set LDFLAGS=-L%PREFIX:\=/%/Library/lib/ -sh ../configure --build=%TRIPLE% ^ - --prefix="%PREFIX:\=/%/Library" ^ - --target=##TARGET## -if errorlevel 1 exit 1 - -make -j4 -if errorlevel 1 exit 1 - -make install -if errorlevel 1 exit 1 - -rem this is a copy of prefixed executables -rmdir /S /Q %PREFIX%\Library\##TARGET## +cd %PREFIX% +tar xjf /c/Users/wfvm/src.tar.bz2 +dir C:\Users\wfvm +python C:\Users\wfvm\patch_prefix.py "##PREFIX##" %PREFIX% diff --git a/artiq-fast/conda-windows/binutils.nix b/artiq-fast/conda-windows/binutils.nix index 6358ebe..b58f9a4 100644 --- a/artiq-fast/conda-windows/binutils.nix +++ b/artiq-fast/conda-windows/binutils.nix @@ -1,4 +1,4 @@ -{ pkgs, version, src, target }: +{ pkgs, version, target, windows-binutils }: let wfvm = import ../wfvm.nix { inherit pkgs; }; @@ -34,11 +34,13 @@ let cp --no-preserve=mode,ownership -R ${./binutils-recipe} binutils sed -i s/##TARGET##/${target}/g binutils/* sed -i s/##VERSION##/${version}/g binutils/* + sed -i 's!##PREFIX##!${windows-binutils.prefix}!g' binutils/* ${wfvm.utils.win-put}/bin/win-put binutils . - tar xjf ${src} - patch -d binutils-${version} -p1 < ${./binutils-hack-libiconv.patch} - tar cjf src.tar.bz2 binutils-${version} + cp ${windows-binutils} src.tar.bz2 ${wfvm.utils.win-put}/bin/win-put src.tar.bz2 . + cp ${./patch_prefix.py} patch_prefix.py + ls -l patch_prefix.py + ${wfvm.utils.win-put}/bin/win-put patch_prefix.py . ${wfvm.utils.win-exec}/bin/win-exec ".\Anaconda3\scripts\activate && conda build --no-anaconda-upload --no-test -c file:///C:/users/wfvm/fake-channel --override-channels binutils" diff --git a/artiq-fast/conda-windows/patch_prefix.py b/artiq-fast/conda-windows/patch_prefix.py new file mode 100644 index 0000000..083d15c --- /dev/null +++ b/artiq-fast/conda-windows/patch_prefix.py @@ -0,0 +1,35 @@ +import os, sys, re + +OLD = sys.argv[1].encode('utf-8') +NEW = sys.argv[2].encode('utf-8') + +if len(NEW) > len(OLD): + raise ValueError("Cannot new shorter than the replacement") +new = NEW + b"\0" * (len(OLD) - len(NEW)) + +def patch_file(path): + with open(path, "r+b") as f: + s = f.read() + occurrences = s.count(OLD) + s = s.replace(OLD, new) + if occurrences > 0: + print("{}: replaced {}, left {}".format(path, occurrences, s.count(OLD))) + with open(path, "w+b") as f: + f.write(s) + +for root, dirs, files in os.walk("."): + for filename in files: + path = "{}/{}".format(root, filename) + patch_file(path) + +def mangle_path(path): + path = re.sub(r"^/c/", "C:/", path) + return str(path) + +OLD = mangle_path(sys.argv[1]).encode('utf-8') +NEW = mangle_path(sys.argv[2]).encode('utf-8') + +for root, dirs, files in os.walk("."): + for filename in files: + path = "{}/{}".format(root, filename) + patch_file(path) diff --git a/artiq-fast/default.nix b/artiq-fast/default.nix index 0569314..d0705ae 100644 --- a/artiq-fast/default.nix +++ b/artiq-fast/default.nix @@ -124,11 +124,17 @@ let }; condaWindowsExperimental = rec { - conda-windows-binutils-or1k = import ./conda-windows/binutils.nix { + windows-binutils-or1k = import ./windows/binutils.nix { inherit pkgs; inherit (mainPackages.binutils-or1k) version src; target = "or1k-linux"; }; + conda-windows-binutils-or1k = import ./conda-windows/binutils.nix { + inherit pkgs; + inherit (mainPackages.binutils-or1k) version; + target = "or1k-linux"; + windows-binutils = windows-binutils-or1k; + }; conda-windows-binutils-arm = import ./conda-windows/binutils.nix { inherit pkgs; inherit (mainPackages.binutils-or1k) version src; diff --git a/artiq-fast/conda-windows/binutils-hack-libiconv.patch b/artiq-fast/windows/binutils-hack-libiconv.patch similarity index 100% rename from artiq-fast/conda-windows/binutils-hack-libiconv.patch rename to artiq-fast/windows/binutils-hack-libiconv.patch diff --git a/artiq-fast/windows/binutils.nix b/artiq-fast/windows/binutils.nix new file mode 100644 index 0000000..0330b81 --- /dev/null +++ b/artiq-fast/windows/binutils.nix @@ -0,0 +1,45 @@ +{ pkgs, version, src, target }: + +let + wfvm = import ../wfvm.nix { inherit pkgs; }; + repeat = s: + let go = n: + if n > 0 + then s + go (n - 1) + else ""; + in go; + outName = "windows-binutils-${target}-${version}.tar.bz2"; + # add padding for later PREFIX replacement in `conda build` + prefix = "/c/Users/wfvm/binutils_" + repeat "0" 80; + build = wfvm.utils.wfvm-run { + name = "build-binutils"; + image = wfvm.makeWindowsImage { installCommands = with wfvm.layers; [ msys2 (msys2-packages (import ../conda-windows/msys_packages.nix { inherit pkgs; } )) ]; }; + script = '' + tar xjf ${src} + patch -d binutils-${version} -p1 < ${./binutils-hack-libiconv.patch} + tar cjf src.tar.bz2 binutils-${version} + ${wfvm.utils.win-put}/bin/win-put src.tar.bz2 . + + cat > build-binutils.bat << EOF + set MSYS=c:\msys64 + set PATH=%MSYS%\usr\bin;%MSYS%\mingw64\bin;%PATH% + tar xjf src.tar.bz2 + bash -c "cd binutils-${version} && ./configure --build=x86_64-pc-mingw64 --prefix=${prefix}/Library --target=${target} && make -j$NIX_BUILD_CORES && make install" + rm -r ${prefix}/Library/or1k-linux + cp %MSYS%/mingw64/bin/libiconv-2.dll ${prefix}/Library/bin/ + tar cjf ${outName} -C ${prefix} . + EOF + ${wfvm.utils.win-put}/bin/win-put build-binutils.bat . + ${wfvm.utils.win-exec}/bin/win-exec build-binutils + + ${wfvm.utils.win-get}/bin/win-get ${outName} + ''; + }; +in +pkgs.runCommand outName { + buildInputs = [ build ]; + passthru = { inherit prefix; }; +} '' + wfvm-run-build-binutils + cp ${outName} $out + ''