Skip to content

Commit

Permalink
📎 Merge to upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
arjxn-py committed Apr 22, 2024
2 parents 752013e + 936e28e commit 7ecd2be
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 52 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
name: sdist
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- name: Set up Python 3.12
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
Expand All @@ -45,7 +45,7 @@ jobs:
name: amd64-windows
runs-on: windows-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: 3.12
Expand Down Expand Up @@ -80,7 +80,7 @@ jobs:
name: amd64-linux
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: 3.12
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
name: arm64-linux
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: 3.12
Expand Down Expand Up @@ -141,7 +141,7 @@ jobs:
name: amd64-macos
runs-on: macos-13
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: 3.12
Expand Down Expand Up @@ -175,7 +175,7 @@ jobs:
name: arm64-macos
runs-on: macos-14
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: 3.12
Expand Down
38 changes: 34 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ concurrency:

env:
FORCE_COLOR: 3
CIBW_BUILD_VERBOSITY: 2
CIBW_BUILD_FRONTEND: "pip" # use pip for now, not build – missing buildDate otherwise

jobs:
style:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: 3.12
Expand All @@ -42,7 +44,7 @@ jobs:
runs-on: macos-14

steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: ${{ matrix.python-version }}
Expand Down Expand Up @@ -76,6 +78,34 @@ jobs:
- name: Test entry points for package
run: nox -s venv

build_wheels_linux_aarch64:
needs: [style]
name: linux-aarch64-buildjet-python-3.12
runs-on: buildjet-2vcpu-ubuntu-2204-arm
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: buildjet/setup-python@v5
- uses: buildjet/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version: '1.22.2'
cache: false
check-latest: true

- name: Build binary distribution (wheel) on Linux (aarch64)
run: |
python -m pip install cibuildwheel
python -m cibuildwheel --output-dir wheelhouse
env:
CIBW_BUILD: "cp312-*"
CIBW_ARCHS_LINUX: aarch64
CIBW_BEFORE_ALL_LINUX: bash scripts/ci/tools/linux/install_go.sh
CIBW_ENVIRONMENT_LINUX: PATH=$PATH:$HOME/go_installed/go/bin
CIBW_SKIP: "pp* *musllinux*"
CIBW_MANYLINUX_AARCH64_IMAGE: "manylinux_2_28"
CIBW_TEST_COMMAND: >
hugo version
hugo env --logLevel debug
experimental:
needs: [style]
name: ${{ matrix.runs-on }}-${{ matrix.architecture }}-python-${{ matrix.python-version }}
Expand All @@ -98,7 +128,7 @@ jobs:
- python-version: "3.13"
runs-on: windows-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: ${{ matrix.python-version }}
Expand Down Expand Up @@ -186,7 +216,7 @@ jobs:
name: inspect-sdist-wheel-contents
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: 3.12
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[build-system]
requires = [
"setuptools>=64",
"pooch",
"tqdm",
"wheel==0.42.0",
# "ziglang>=0.11.0"
]
Expand Down
79 changes: 39 additions & 40 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,22 @@
import shutil
import subprocess
import sys
import tarfile
from pathlib import Path

import pooch
from setuptools import Command, Extension, setup
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
from wheel.bdist_wheel import bdist_wheel

HUGO_VERSION = "0.125.1"
HUGO_RELEASE = (
f"https://github.com/gohugoio/hugo/archive/refs/tags/v{HUGO_VERSION}.tar.gz"
)
# Commit hash for current HUGO_VERSION, needs to be updated when HUGO_VERSION is updated
# Tip: git ls-remote --tags https://github.com/gohugoio/hugo v<HUGO_VERSION>
HUGO_RELEASE_COMMIT_HASH = "68c5ad638c2072969e47262926b912e80fd71a77"
# The pooch tool will download the tarball into the hugo_cache/ directory.
# ------ Hugo build configuration and constants ------------------------------------

HUGO_VERSION = "0.125.2"
# The Go toolchain will download the tarball into the hugo_cache/ directory.
# We will point the build command to that location to build Hugo from source
HUGO_CACHE_DIR = "hugo_cache"
HUGO_SHA256 = "dc902d7a2983925b218e17d21e510867a97e0b8c0e0093e4a6a22dabdb21c230"
FILE_EXT = (
".exe" if (sys.platform == "win32" or os.environ.get("GOOS") == "windows") else ""
)

# The vendor name is used to set the vendorInfo variable in the Hugo binary
HUGO_VENDOR_NAME = "hugo-python-distributions"

Expand All @@ -53,6 +45,8 @@
+ FILE_EXT
)

# ----------------------------------------------------------------------------------


class HugoWriter(build_py):
"""
Expand Down Expand Up @@ -112,18 +106,9 @@ def run(self):
# the name so that it is unique to the version of Hugo being built.
"""

# Download Hugo source tarball, place into hugo_cache/ directory
hugo_targz = pooch.retrieve(
url=HUGO_RELEASE,
known_hash=HUGO_SHA256,
path=HUGO_CACHE_DIR,
progressbar=True,
)

# Extract Hugo source tarball into a folder hugo-HUGO_VERSION/
# inside hugo_cache/
with tarfile.open(hugo_targz) as tar:
tar.extractall(path=HUGO_CACHE_DIR)
# If Hugo cache does not exist, create it
if not Path(HUGO_CACHE_DIR).exists():
Path(HUGO_CACHE_DIR).mkdir(parents=True)

# The binary is put into GOBIN, which is set to the package directory
# (hugo/binaries/) for use in editable mode. The binary is copied
Expand All @@ -135,12 +120,12 @@ def run(self):
# os.path.dirname(os.path.abspath(__file__)), "hugo", "binaries"
# )
os.environ["CGO_ENABLED"] = "1"
os.environ["GOPATH"] = os.path.abspath(HUGO_CACHE_DIR) # noqa: PTH100
os.environ["GOPATH"] = str(Path(HUGO_CACHE_DIR).resolve())
# it must be absolute (Go requirement)

# Set GOCACHE to the hugo_cache/ directory so that the Go toolchain
# caches the build artifacts there for future use.
os.environ["GOCACHE"] = os.path.abspath(HUGO_CACHE_DIR) # noqa: PTH100
os.environ["GOCACHE"] = str(Path(HUGO_CACHE_DIR).resolve())

os.environ["GOOS"] = os.environ.get("GOOS", self.hugo_platform)
os.environ["GOARCH"] = os.environ.get("GOARCH", self.hugo_arch)
Expand All @@ -155,15 +140,11 @@ def run(self):
#
# Once built this the files are cached into GOPATH for future use

# Delete hugo_cache/bin/ + files inside, it left over from a previous build
# Delete hugo_cache/bin/ + files inside, if left over from a previous build
shutil.rmtree(Path(HUGO_CACHE_DIR).resolve() / "bin", ignore_errors=True)

# ldflags are passed to the go linker to set variables at runtime
# Note: the Homebrew version of Hugo sets extra ldflags such as the build
# date. We do not set that here, we only set the vendorInfo variable.
ldflags = [
f"-X github.com/gohugoio/hugo/common/hugo.vendorInfo={HUGO_VENDOR_NAME}",
]
# shutil.rmtree(
# Path(HUGO_CACHE_DIR).resolve() / f"hugo-{HUGO_VERSION}", ignore_errors=True
# )

# Check for compilers, toolchains, etc. and raise helpful errors if they
# are not found. These are essentially smoke tests to ensure that the
Expand Down Expand Up @@ -202,16 +183,35 @@ def run(self):
error_message = "Git not found. Please install Git from https://git-scm.com/downloads or your package manager."
raise OSError(error_message) from err

# These ldflags are passed to the Go linker to set variables at runtime
ldflags = [
f"-s -w -X github.com/gohugoio/hugo/common/hugo.vendorInfo={HUGO_VENDOR_NAME}"
]

if not (Path(HUGO_CACHE_DIR).resolve() / f"hugo-{HUGO_VERSION}").exists():
subprocess.check_call(
[
"git",
"clone",
"https://github.com/gohugoio/hugo.git",
"--depth=1",
"--single-branch",
"--branch",
f"v{HUGO_VERSION}",
Path(HUGO_CACHE_DIR) / f"hugo-{HUGO_VERSION}",
]
)

subprocess.check_call(
[
"go",
"install",
"-ldflags",
*ldflags,
" ".join(ldflags),
"-tags",
"extended",
],
cwd=os.path.abspath(os.path.join(HUGO_CACHE_DIR, f"hugo-{HUGO_VERSION}")), # noqa: PTH118, PTH100
cwd=(Path(HUGO_CACHE_DIR) / f"hugo-{HUGO_VERSION}").resolve(),
)
# TODO: introduce some error handling here to detect compilers, etc.

Expand Down Expand Up @@ -321,13 +321,12 @@ def run(self):


# Mock setuptools into thinking that we are building a target binary on a host machine
# so that the wheel gets tagged correctly. We can fuse the arm64 and amd64 wheels
# together later using delocate.
# so that the wheel gets tagged correctly when building or cross-compiling.
class HugoWheel(bdist_wheel):
"""
A customised wheel build command that sets the platform tags to accommodate
the varieties of the GOARCH and GOOS environment variables when cross-compiling
the Hugo binary. Currently used for macOS arm64 and macOS x86_64.
the Hugo binary with any available cross-compilation toolchain.
"""

def initialize_options(self):
Expand Down Expand Up @@ -408,7 +407,7 @@ def run(self):
Path(__file__).parent
/ "hugo"
/ "binaries"
/ f"{os.environ.get('GOOS', HUGO_PLATFORM)}_{os.environ.get('GOARCH', HUGO_ARCH)}{FILE_EXT}"
/ f"hugo-{HUGO_VERSION}-{os.environ.get('GOOS', HUGO_PLATFORM)}-{os.environ.get('GOARCH', HUGO_ARCH)}{FILE_EXT}"
)

# if the binary does not exist, then we need to build it, so invoke
Expand Down

0 comments on commit 7ecd2be

Please sign in to comment.