Skip to content

Commit

Permalink
feat: add pyproject support
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Jun 7, 2024
1 parent 988c779 commit cb453cf
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 47 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
workflow_dispatch:
pull_request:
paths:
- '**.py'
- '**.yml'
- '**.toml'
push:
branches:
- main
paths:
- '**.py'
- '**.yml'
- '**.toml'

jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install
run: |
pip install hatch
pip install ruff
- name: lint
run: |
make lint
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release on Pypi

on:
workflow_dispatch:
push:
tags:
- 'v*'

jobs:
pypi-publish:
name: Upload release to PyPI
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install pypa
run: >-
pip install hatch
- name: Install
uses: pandoc/actions/setup@main
- name: Pulish
run: |
hatch build
hatch publish -u '__token__' -a '${{ secrets.HATCH_INDEX_AUTH }}'
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml → .github/workflows/zig.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Test zig

on:
workflow_dispatch:
Expand All @@ -13,7 +13,7 @@ on:

env:
OUTPUT: /tmp/cc.md
ZIGCC_LOG: 1
ZIGCC_VERBOSE: 1

# Allow one concurrent deployment
concurrency:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target
cgo-demo
dist
README.md
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
build: clean readme
hatch build

clean:
rm -rf build dist shgpt.egg-info

fix:
ruff check --fix
ruff format

lint:
ruff check
ruff format --check

readme:
pandoc -f org -t markdown README.org -o README.md

.PHONY: build clean fix lint readme
31 changes: 14 additions & 17 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
* Zig-cc
[[https://github.com/jiacai2050/zig-cc/actions/workflows/CI.yml][https://github.com/jiacai2050/zig-cc/actions/workflows/CI.yml/badge.svg]]
* zigcc
[[https://github.com/jiacai2050/zig-cc/actions/workflows/ci.yml][https://github.com/jiacai2050/zig-cc/actions/workflows/ci.yml/badge.svg]]
[[https://github.com/jiacai2050/zig-cc/actions/workflows/zig.yml][https://github.com/jiacai2050/zig-cc/actions/workflows/zig.yml/badge.svg]]
[[https://github.com/jiacai2050/zig-cc/actions/workflows/release.yml][https://github.com/jiacai2050/zig-cc/actions/workflows/release.yml/badge.svg]]
[[https://pypi.org/project/zigcc][https://img.shields.io/pypi/v/zigcc]]

A util script that aims to ease use =zig cc= to compile C/C++/Rust/Go programs.

Expand All @@ -17,27 +20,21 @@ So this project was born, it will
- Convert target between Zig and Rust/Go
- Ignore link args when =zig cc= throw errors, hopefully this will make compile successfully, WIP.
* Install
Download [[file:zigcc][zigcc]] file, give it executable permission, and create two soft links =zigcxx=, =zigcargo= to it.
#+begin_src bash
curl -Lo zigcc https://github.com/jiacai2050/zig-cc/raw/main/zigcc
chmod +x zigcc
ln -s zigcc zigcxx
ln -s zigcc zigcargo
pip install -U zigcc
#+end_src

Then use like this
#+begin_src bash
CC=zigcc CXX=zigcxx ...
#+end_src

=zigcargo= can used to replace =cargo=, it will automatically set
- =CC= =CARGO_TARGET_<triple>_LINKER= to =zigcc=
- =CXX= to =zigcxx=
This will install three executables:
- =zigcc=, used for =CC=
- =zigcxx=, used for =CXX=
- =zigcargo= can used to replace =cargo=, it will automatically set
- =CC= =CARGO_TARGET_<triple>_LINKER= to =zigcc=
- =CXX= to =zigcxx=

* Use in GitHub Action
Adding a step to your workflow like this:
#+begin_src yaml
- uses: jiacai2050/zigcc@v1
- uses: jiacai2050/zigcc@main
with:
zig-version: master
#+end_src
Expand All @@ -50,4 +47,4 @@ There some are env variable to config zigcc:
#+begin_src bash
= note: error: unsupported linker arg: -dylib
#+end_src
- =ZIGCC_LOG= Set to =1= enable verbose logs.
- =ZIGCC_VERBOSE= Set to =1= enable verbose logs.
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ runs:
with:
version: ${{ inputs.zig-version }}
- name: Set GitHub Path
run: echo "${GITHUB_WORKSPACE}/.jiacai2050-zigcc" >> $GITHUB_PATH
run: |
my_dir="${GITHUB_WORKSPACE}/.jiacai2050-zigcc"
echo "${my_dir}" >> $GITHUB_PATH
cp "${my_dir}/zigcc/__init__.py" "${my_dir}/zigcc"
ln -s "${my_dir}/zigcc" "${my_dir}/zigcxx"
ln -s "${my_dir}/zigcc" "${my_dir}/zigcargo"
shell: bash
61 changes: 61 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "zigcc"
dynamic = ["version"]
authors = [{name = "Jiacai Liu", email="dev@liujiacai.net"}]
description = "A util script that aims to ease use `zig cc` to compile C/C++/Rust/Go programs."
readme = "README.md"
keywords = ["zig", "compile", "rust", "cross-compile"]
license = "GPL-3.0"
requires-python = ">=3.0.0"
dependencies = [
]
classifiers = [
"Operating System :: OS Independent",
"Topic :: Software Development",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Intended Audience :: Information Technology",
"Intended Audience :: System Administrators",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]

[project.urls]
Repository = "https://github.com/jiacai2050/zigcc"
Issues = "https://github.com/jiacai2050/zigcc/issues"

[project.scripts]
zigcc = "zigcc:main"
zigcxx = "zigcc:main"
zigcargo = "zigcc:main"

[tool.hatch.version]
path = "zigcc/__init__.py"

[tool.hatch.build.targets.sdist]
include = [
"zigcc",
"pyproject.toml",
]

[tool.hatch.build.targets.wheel]
include = [
"zigcc"
]

# https://github.com/astral-sh/ruff?tab=readme-ov-file#configuration
[tool.ruff.lint]
fixable = ["ALL"]

[tool.ruff.format]
quote-style = "single"
1 change: 0 additions & 1 deletion zigcargo

This file was deleted.

66 changes: 41 additions & 25 deletions zigcc → zigcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,43 @@
# 3. zigcargo, invoke `cargo` with `CC`, `CXX`, and rustc linker set to zig.
#
# Supported env vars:
# 1. `ZIGCC_LOG`, enable log
# 1. `ZIGCC_VERBOSE`, enable verbose log
# 2. `ZIGCC_FLAGS`, extra flags passed to zig, such as `-fno-sanitize=undefined`

import sys, os
import sys
import os
import logging
import subprocess
import platform

__VERSION__ = '0.1.0'

UNKNOWN = 0
RUST = 1
GO = 2
ENABLE_LOG = os.getenv('ZIGCC_LOG') in ['true', '1']
IS_MACOS = platform.system() == 'Darwin'
ENABLE_LOG = os.getenv('ZIGCC_VERBOSE', '0') == '1'
FLAGS = os.getenv('ZIGCC_FLAGS', '').split(' ')
FLAGS = [f for f in FLAGS if f != '']

# Blacklist flags, wild match
BLACKLIST_WILD_FLAGS = os.getenv('ZIGCC_BLACKLIST_FLAGS', '').split(' ') + ['--target', '-exported_symbols_list',
'-no_pie', '-Wl,-dylib',
# https://github.com/ziglang/zig/issues/5320
'self-contained/rcrt1.o',
'self-contained/crti.o',
]
BLACKLIST_WILD_FLAGS = os.getenv('ZIGCC_BLACKLIST_FLAGS', '').split(' ') + [
'--target',
'-exported_symbols_list',
'-no_pie',
'-Wl,-dylib',
# https://github.com/ziglang/zig/issues/5320
'self-contained/rcrt1.o',
'self-contained/crti.o',
]
BLACKLIST_WILD_FLAGS = [f for f in BLACKLIST_WILD_FLAGS if f != '']


def log(msg, *args, **kwargs):
if ENABLE_LOG:
logging.info(msg, *args, **kwargs)


def zig_target_from(target, lang):
if lang == RUST:
# Zig target has no vendor field
Expand All @@ -55,20 +64,16 @@ def zig_target_from(target, lang):
zig_arch = {
'i686': 'x86',
}.get(arch, arch)
zig_os = {
'darwin': 'macos'
}.get(os, os)
zig_os = {'darwin': 'macos'}.get(os, os)
return '-'.join([zig_arch, zig_os, abi])
elif lang == GO:
[arch, os] = target.split('-', 2)
zig_arch = {
"386": "x86",
"amd64": "x86_64",
"arm64": "aarch64",
'386': 'x86',
'amd64': 'x86_64',
'arm64': 'aarch64',
}.get(arch, arch)
zig_os = {
'darwin': 'macos'
}.get(os, os)
zig_os = {'darwin': 'macos'}.get(os, os)
return '-'.join([zig_arch, zig_os])
else:
return target
Expand All @@ -91,6 +96,7 @@ def detect_zig_target():
def cargo_linker_var_name(target):
return 'CARGO_TARGET_{}_LINKER'.format(target.replace('-', '_').upper())


def guess_rust_target(args):
found_target = False
for arg in args:
Expand All @@ -116,16 +122,16 @@ def guess_rust_target(args):
log('Get rustc triple failed, err:{}', e)
raise e


def run_subprocess(args, env):
log('Begin run command\nArgs:%s\nEnv:%s', args, env)
try:
subprocess.run(args,
check=True,
env=env)
subprocess.run(args, check=True, env=env)
except subprocess.CalledProcessError as e:
log(f'Command {e.cmd} failed with error {e.returncode}')
sys.exit(e.returncode)


def run_as_cargo(args):
target = guess_rust_target(args)
env = os.environ.copy()
Expand All @@ -134,9 +140,9 @@ def run_as_cargo(args):
env['CXX'] = 'zigcxx'
run_subprocess(['cargo'] + args, env)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(message)s')

def main():
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
program = os.path.basename(sys.argv[0])
args = sys.argv[1:]
if program == 'zigcargo':
Expand All @@ -148,7 +154,7 @@ def run_as_cargo(args):
'zigcxx': ['zig', 'c++'],
}.get(program)
if run_args is None:
print(f"Unknown program, {program}")
print(f'Unknown program, {program}')
sys.exit(1)

target = detect_zig_target()
Expand All @@ -158,6 +164,12 @@ def run_as_cargo(args):
for flag in FLAGS:
run_args.append(flag)

# https://github.com/ziglang/zig/issues/10299#issuecomment-989736808
# Append $(xcrun --show-sdk-path)/System/Library/Frameworks in search path
if IS_MACOS:
root_path = subprocess.getoutput('xcrun --show-sdk-path')
run_args += [f'-F{root_path}/System/Library/Frameworks']

for arg in args:
found = False
for wild_args in BLACKLIST_WILD_FLAGS:
Expand All @@ -171,3 +183,7 @@ def run_as_cargo(args):
run_args.append(arg)

run_subprocess(run_args, os.environ)


if __name__ == '__main__':
main()
1 change: 0 additions & 1 deletion zigcxx

This file was deleted.

0 comments on commit cb453cf

Please sign in to comment.