Skip to content

Commit

Permalink
Build Conda packages
Browse files Browse the repository at this point in the history
Add `paths_version`
  • Loading branch information
bblanchon committed Sep 23, 2023
1 parent 53d8ed0 commit f99cd30
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 1 deletion.
50 changes: 50 additions & 0 deletions .github/workflows/build-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,56 @@ jobs:
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}

conda:
name: Build Conda packages
if: github.event.inputs.version
runs-on: ubuntu-latest
needs:
- linux_arm
- linux_arm64
- linux_x64
- linux_x86
- mac_arm64
- mac_x64
- win_arm64
- win_x64
- win_x86
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Download artifacts
uses: actions/download-artifact@v3
- name: Create build script
run: |
cat >conda-build.sh <<END
#!/bin/bash -eux
python conda/build.py pdfium-binaries \$1 ${{ github.event.inputs.version }} --about-file conda/about.json --source-dir \$2 --output-dir conda/out
END
chmod +x conda-build.sh
- name: Build for linux-32
run: ./conda-build.sh linux-32 pdfium-linux-x86
- name: Build for linux-64
run: ./conda-build.sh linux-64 pdfium-linux-x64
- name: Build for linux-aarch64
run: ./conda-build.sh linux-aarch64 pdfium-linux-arm64
- name: Build for linux-armv7l
run: ./conda-build.sh linux-armv7l pdfium-linux-arm
- name: Build for osx-arm64
run: ./conda-build.sh osx-arm64 pdfium-mac-arm64
- name: Build for osx-64
run: ./conda-build.sh osx-64 pdfium-mac-x64
- name: Build for win-32
run: ./conda-build.sh win-32 pdfium-win-x86
- name: Build for win-64
run: ./conda-build.sh win-64 pdfium-win-x64
- name: Build for win-arm64
run: ./conda-build.sh win-arm64 pdfium-win-arm64
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: Conda packages
path: conda/out/

publish:
name: Publish Release
if: startsWith(github.event.inputs.branch, 'chromium/') && github.event.inputs.version && github.event.inputs.publish == 'true'
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
/build/
*.tgz
/*-musl-cross/
/android-ndk*
/android-ndk*
/conda/out/
9 changes: 9 additions & 0 deletions conda/about.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"summary": "PDFium binaries",
"description": "PDFium is an open-source library for PDF manipulation and rendering.",
"license": "Apache-2.0 OR BSD-3-Clause (+ third party licenses)",
"home": "https://github.com/bblanchon/pdfium-binaries",
"dev_url": "https://github.com/bblanchon/pdfium-binaries",
"doc_url": "https://developers.foxit.com/resources/pdf-sdk/c_api_reference_pdfium/index.html",
"license_url": "https://pdfium.googlesource.com/pdfium/"
}
146 changes: 146 additions & 0 deletions conda/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/usr/bin/env python
#
# Simple replacement for conda-build
# Builds a package according to the conda package specification
# https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/pkg-specs.html
# https://docs.conda.io/projects/conda-build/en/stable/resources/package-spec.html

import json
import re
import tarfile
from argparse import ArgumentParser
from hashlib import sha256
from pathlib import Path
from tempfile import TemporaryDirectory

CONDA_PLATFORMS = {
"linux-32": ("linux", "x86"),
"linux-64": ("linux", "x86_64"),
"linux-aarch64": ("linux", "aarch64"),
"linux-armv6l": ("linux", "armv6l"),
"linux-armv7l": ("linux", "armv7l"),
"linux-ppc64": ("linux", "ppc64"),
"linux-ppc64le": ("linux", "ppc64le"),
"linux-s390x": ("linux", "s390x"),
"osx-arm64": ("osx", "arm64"),
"osx-64": ("osx", "x86_64"),
"win-32": ("win", "x86"),
"win-64": ("win", "x86_64"),
"win-arm64": ("win", "arm64"),
}

SOURCE_GLOBS = [
"bin/*.dll",
"lib/*.so",
"lib/*.lib",
"lib/*.dylib",
"include/**/*.h",
]


def create_json_file(filename: str, data: dict):
with open(filename, "w") as file:
json.dump(data, file, indent=2)


def conda_build(
package: str,
platform: str,
version: str,
build: str,
build_number: int,
source_dir: Path,
output_dir: Path,
about_file: Path,
):
assert "-" not in build, f"Invalid build {build!r}"
assert build_number > 0, f"Invalid build number {build_number}"
assert source_dir.exists(), f"Source directory {source_dir} does not exist"
assert re.match(
r"^\d+\.\d+\.\d+(?:.\d+)?$", version
), f"Invalid version {version!r}"

output_file = output_dir / platform / f"{package}-{version}-{build_number}.tar.bz2"
output_file.parent.mkdir(parents=True, exist_ok=True)

assert platform in CONDA_PLATFORMS, f"Unknown platform {platform!r}"
operating_system, architecture = CONDA_PLATFORMS[platform]

files: list[Path] = []

with tarfile.open(output_file, "w:bz2") as tar:
for glob in SOURCE_GLOBS:
for file in source_dir.glob(glob):
tar.add(file, file.relative_to(source_dir))
files.append(file)
assert files, f"No files found for globs {SOURCE_GLOBS}"

with TemporaryDirectory() as info_dir:
create_json_file(
f"{info_dir}/index.json",
{
"arch": architecture,
"build_number": build_number,
"build": build,
"depends": [],
"name": package,
"platform": operating_system,
"version": version,
},
)

Path(info_dir, "files").write_text(
"\n".join(
str(file.relative_to(source_dir).as_posix()) for file in files
)
)

create_json_file(
f"{info_dir}/paths.json",
{
"paths": [
{
"_path": str(file.relative_to(source_dir).as_posix()),
"path_type": "hardlink",
"sha256": sha256(file.read_bytes()).hexdigest(),
"size_in_bytes": file.stat().st_size,
}
for file in files
],
"paths_version": 1,
},
)

if about_file:
assert about_file.exists(), f"About file {about_file} does not exist"
tar.add(about_file, "info/about.json")

license_file = source_dir / "LICENSE"
if license_file.exists():
tar.add(license_file, "info/license.txt")

tar.add(info_dir, "info")

return output_file


def main():
parser = ArgumentParser(description="Build conda packages.")
parser.add_argument("package", help="Package name")
parser.add_argument("platform", help="Platform")
parser.add_argument("version", help="Version")
parser.add_argument("--source-dir", help="Source directory", type=Path, default=".")
parser.add_argument("--output-dir", help="Output directory", type=Path, default=".")
parser.add_argument("--about-file", help="Path to about.json", type=Path)
parser.add_argument("--build", help="The build string", default="")
parser.add_argument("--build-number", help="The build number", type=int, default=1)

args = parser.parse_args()

output_file = conda_build(**vars(args))

print(f"Built package {output_file}")


if __name__ == "__main__":
main()

0 comments on commit f99cd30

Please sign in to comment.