Skip to content

Commit

Permalink
fix: zig 0.11 (#62)
Browse files Browse the repository at this point in the history
* build.zig: enable headerpad_max_install_names on MacOs

Fixes error during packing on MacOS with Homebrew:
Error: Failed changing dylib ID of /usr/local/Cellar/fastfec/0.1.9/lib/libfastfec.dylib
  from @rpath/libfastfec.dylib
    to /usr/local/opt/fastfec/lib/libfastfec.dylib
Error: Failed to fix install linkage

and

  Error: Failed changing install name in /opt/homebrew/Cellar/fastfec/0.1.9/bin/fastfec
    from /opt/homebrew/opt/pcre/lib/libpcre.1.dylib
      to @@HOMEBREW_PREFIX@@/opt/pcre/lib/libpcre.1.dylib

See ziglang/zig#13388, where this is inspired from

* Updated for changes to zig build system in version 11

* add back preferred optimize mode

* change wasm os target to freestanding to fix missing main symbol error

* feat: update zig version in github actions workflows

* chore: remove jira ticket from template

* chore: std=gnu99

* chore: unistd for isatty

* chore: include unistd always

* chore: pin python version, update wheel

* chore: attempt to fix python wheel

* chore: remove reproducible wheel

* chore: os-specific python setup

* chore: log lib

* chore: blanket ls

* chore: zig cache ls

* chore: add verbosity

* fix: update zig in requirements-dev

* chore: zig 0.11.0 everywhere else

* chore: revert setup change

* chore: remove test cmds from workflow

* chore: 3.11

---------

Co-authored-by: Michka Popoff <[email protected]>
Co-authored-by: James Christensen <[email protected]>
  • Loading branch information
3 people authored Feb 28, 2024
1 parent 18d32eb commit 89e1fd6
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 52 deletions.
2 changes: 0 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Information about what you changed for this PR

## Link to Jira Ticket

## Test Steps

## After Screenshot(s)
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/_python-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: "3.x"
python-version: "3.11"

- name: Install Zig
run: python -m pip install ziglang==0.9.1 wheel==0.37.1
run: python -m pip install ziglang==0.11.0 wheel

- name: Build wheels
run: python python/make_wheels.py ${{ matrix.architecture }}-${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.9.1
version: 0.11.0
- name: Get output paths
uses: kanga333/variable-mapper@master
id: map
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.9.1
version: 0.11.0
- name: Run zig test
run: zig build test

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ __pycache__
*.so
*.dylib
*.dll
*.lib
*.pdb

# Distribution / packaging
.Python
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The following was performed on an M1 Macbook Air:

### Build system

[Zig](https://ziglang.org/) is used to build and compile the project. Download and install the latest version of Zig (>=0.9.1) by following the instructions on the website (you can verify it's working by typing `zig` in the terminal and seeing help commands).
[Zig](https://ziglang.org/) is used to build and compile the project. Download and install the latest version of Zig (>=0.11.0) by following the instructions on the website (you can verify it's working by typing `zig` in the terminal and seeing help commands).

### Dependencies

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.9
0.2.0
67 changes: 47 additions & 20 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ pub fn linkPcre(vendored_pcre: bool, libExe: *std.build.LibExeObjStep) void {
libExe.linkSystemLibrary("libpcre");
}
}
if (libExe.target.isDarwin()) {
// useful for package maintainers
// see https://github.com/ziglang/zig/issues/13388
libExe.headerpad_max_install_names = true;
}
}

pub fn build(b: *std.build.Builder) !void {
b.setPreferredReleaseMode(.ReleaseFast);
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const optimize = b.standardOptimizeOption(.{
.preferred_optimize_mode = .ReleaseFast,
});

const lib_only: bool = b.option(bool, "lib-only", "Only compile the library") orelse false;
const skip_lib: bool = b.option(bool, "skip-lib", "Skip compiling the library") orelse false;
Expand All @@ -26,10 +32,11 @@ pub fn build(b: *std.build.Builder) !void {

// Main build step
if (!lib_only and !wasm) {
const fastfec_cli = b.addExecutable("fastfec", null);
fastfec_cli.setTarget(target);
fastfec_cli.setBuildMode(mode);
fastfec_cli.install();
const fastfec_cli = b.addExecutable(.{
.name = "fastfec",
.target = target,
.optimize = optimize,
});

fastfec_cli.linkLibC();

Expand All @@ -39,40 +46,59 @@ pub fn build(b: *std.build.Builder) !void {
"src/cli.c",
"src/main.c",
}, &buildOptions);
b.installArtifact(fastfec_cli);
}

if (!wasm and !skip_lib) {
// Library build step
const fastfec_lib = b.addSharedLibrary("fastfec", null, .unversioned);
fastfec_lib.setTarget(target);
fastfec_lib.setBuildMode(mode);
fastfec_lib.install();
const fastfec_lib = b.addSharedLibrary(.{
.name = "fastfec",
.target = target,
.optimize = optimize,
.version = null,
});
if (fastfec_lib.target.isDarwin()) {
// useful for package maintainers
// see https://github.com/ziglang/zig/issues/13388
fastfec_lib.headerpad_max_install_names = true;
}
fastfec_lib.linkLibC();
fastfec_lib.addCSourceFiles(&libSources, &buildOptions);
linkPcre(vendored_pcre, fastfec_lib);
b.installArtifact(fastfec_lib);
} else if (wasm) {
// Wasm library build step
const fastfec_wasm = b.addSharedLibrary("fastfec", null, .unversioned);
const wasm_target = CrossTarget{ .cpu_arch = .wasm32, .os_tag = .wasi };
fastfec_wasm.setTarget(wasm_target);
fastfec_wasm.setBuildMode(mode);
fastfec_wasm.install();
const wasm_target = CrossTarget{ .cpu_arch = .wasm32, .os_tag = .freestanding };
const fastfec_wasm = b.addSharedLibrary(.{
.name = "fastfec",
.target = wasm_target,
.optimize = optimize,
.version = null,
});
fastfec_wasm.linkLibC();
fastfec_wasm.addCSourceFiles(&libSources, &buildOptions);
linkPcre(vendored_pcre, fastfec_wasm);
fastfec_wasm.addCSourceFile("src/wasm.c", &buildOptions);
fastfec_wasm.addCSourceFile(.{ .file = .{
.path = "src/wasm.c",
}, .flags = &buildOptions });
b.installArtifact(fastfec_wasm);
}

// Test step
var prev_test_step: ?*std.build.Step = null;
for (tests) |test_file| {
const base_file = std.fs.path.basename(test_file);
const subtest_exe = b.addExecutable(base_file, null);
const subtest_exe = b.addExecutable(.{
.name = base_file,
});
subtest_exe.linkLibC();
subtest_exe.addCSourceFiles(&testIncludes, &buildOptions);
linkPcre(vendored_pcre, subtest_exe);
subtest_exe.addCSourceFile(test_file, &buildOptions);
const subtest_cmd = subtest_exe.run();
subtest_exe.addCSourceFile(.{
.file = .{ .path = test_file },
.flags = &buildOptions,
});
const subtest_cmd = b.addRunArtifact(subtest_exe);
if (prev_test_step != null) {
subtest_cmd.step.dependOn(prev_test_step.?);
}
Expand Down Expand Up @@ -119,6 +145,7 @@ const testIncludes = [_][]const u8{ "src/buffer.c", "src/memory.c", "src/encodin
const buildOptions = [_][]const u8{
"-std=c11",
"-pedantic",
"-std=gnu99",
"-Wall",
"-W",
"-Wno-missing-field-initializers",
Expand Down
21 changes: 5 additions & 16 deletions python/make_wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@
version = f.read().strip()


class ReproducibleWheelFile(WheelFile):
# Copied from Zig make_wheels.py
def writestr(self, zinfo, *args, **kwargs):
if not isinstance(zinfo, ZipInfo):
raise ValueError("ZipInfo required")
zinfo.date_time = time.gmtime(time.time())[0:6] # Current time
zinfo.create_system = 3
super().writestr(zinfo, *args, **kwargs)


def make_message(headers, payload=None):
# Copied from Zig make_wheels.py
msg = EmailMessage()
Expand All @@ -72,8 +62,7 @@ def make_message(headers, payload=None):


def write_wheel_file(filename, contents):
# Copied from Zig make_wheels.py
with ReproducibleWheelFile(filename, "w") as wheel:
with WheelFile(filename, "w") as wheel:
for member_info, member_source in contents.items():
if not isinstance(member_info, ZipInfo):
member_info = ZipInfo(member_info)
Expand Down Expand Up @@ -118,9 +107,9 @@ def write_wheel(out_dir, *, name, version, tag, metadata, description, contents)
for path in glob(os.path.join(SRC_DIR, "*.py"), recursive=True):
with open(path, "rb") as f:
file_contents = f.read()
base_contents[
os.path.join(PACKAGE_NAME, os.path.relpath(path, SRC_DIR))
] = file_contents
base_contents[os.path.join(PACKAGE_NAME, os.path.relpath(path, SRC_DIR))] = (
file_contents
)

current_platform = platform.system()
for target_platform, zig_target, wheel_platform in matrix:
Expand All @@ -129,7 +118,7 @@ def write_wheel(out_dir, *, name, version, tag, metadata, description, contents)
# First clear the target directory of any stray files
if os.path.exists(LIBRARY_DIR):
shutil.rmtree(LIBRARY_DIR)
# Compile! Requires ziglang==0.9.1 to be installed
# Compile! Requires ziglang==0.11.0 to be installed
subprocess.call(
[
sys.executable,
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
requires = [
"setuptools",
"wheel",
"ziglang==0.9.1"
"ziglang==0.11.0"
]
2 changes: 1 addition & 1 deletion python/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pytest-xdist
pytest-mock
black
isort
ziglang==0.9.1
ziglang==0.11.0
-e .
8 changes: 5 additions & 3 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@


def compile_library():
subprocess.call([sys.executable, "-m", "ziglang", "build", "-Dlib-only=true"], cwd=PARENT_DIR)
subprocess.call(
[sys.executable, "-m", "ziglang", "build", "-Dlib-only=true"], cwd=PARENT_DIR
)


compile_library()
Expand All @@ -33,7 +35,8 @@ def compile_library():
)
# Copy them into the Python project src directory and get their relative paths
library_files = [
os.path.basename(shutil.copy(library_file, os.path.join(CURRENT_DIR, "src"))) for library_file in raw_library_files
os.path.basename(shutil.copy(library_file, os.path.join(CURRENT_DIR, "src")))
for library_file in raw_library_files
]

# Force building a non-pure lib wheel
Expand All @@ -46,7 +49,6 @@ def finalize_options(self):
_bdist_wheel.finalize_options(self)
self.root_is_pure = False


except ImportError:
bdist_wheel = None

Expand Down
15 changes: 13 additions & 2 deletions python/src/fastfec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
import logging
import os
import pathlib
from ctypes import CFUNCTYPE, POINTER, c_char, c_char_p, c_int, c_size_t, c_void_p, memmove
from ctypes import (
CFUNCTYPE,
POINTER,
c_char,
c_char_p,
c_int,
c_size_t,
c_void_p,
memmove,
)
from glob import glob

logger = logging.getLogger("fastfec")
Expand Down Expand Up @@ -216,7 +225,9 @@ def write_callback(filename, extension, contents, num_bytes):
file_descriptor = write_cache.file_descriptors.get(path)
if not file_descriptor:
# Open the file
write_cache.file_descriptors[path] = open_function(path.decode("utf8"), mode="wb")
write_cache.file_descriptors[path] = open_function(
path.decode("utf8"), mode="wb"
)
file_descriptor = write_cache.file_descriptors[path]
write_cache.last_filename = filename
write_cache.last_fd = file_descriptor
Expand Down
2 changes: 1 addition & 1 deletion python/tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist=py{3}
envlist=py{3.11}
skipsdist=True

[base]
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "encoding.h"
#include "fec.h"
#include "cli.h"
#include <unistd.h>

#define BUFFERSIZE 65536

Expand Down

0 comments on commit 89e1fd6

Please sign in to comment.