zigcc 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
zigcc-0.1.0/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ target
2
+ cgo-demo
3
+ dist
4
+ README.md
zigcc-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Jiacai Liu <dev@liujiacai.net>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
zigcc-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,111 @@
1
+ Metadata-Version: 2.3
2
+ Name: zigcc
3
+ Version: 0.1.0
4
+ Summary: A util script that aims to ease use `zig cc` to compile C/C++/Rust/Go programs.
5
+ Project-URL: Repository, https://github.com/jiacai2050/zigcc
6
+ Project-URL: Issues, https://github.com/jiacai2050/zigcc/issues
7
+ Author-email: Jiacai Liu <dev@liujiacai.net>
8
+ License-Expression: GPL-3.0
9
+ License-File: LICENSE
10
+ Keywords: compile,cross-compile,rust,zig
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Information Technology
13
+ Classifier: Intended Audience :: System Administrators
14
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.6
19
+ Classifier: Programming Language :: Python :: 3.7
20
+ Classifier: Programming Language :: Python :: 3.8
21
+ Classifier: Programming Language :: Python :: 3.9
22
+ Classifier: Programming Language :: Python :: 3.10
23
+ Classifier: Programming Language :: Python :: 3.11
24
+ Classifier: Topic :: Software Development
25
+ Requires-Python: >=3.0.0
26
+ Description-Content-Type: text/markdown
27
+
28
+ # zigcc
29
+
30
+ [![](https://github.com/jiacai2050/zig-cc/actions/workflows/ci.yml/badge.svg)](https://github.com/jiacai2050/zig-cc/actions/workflows/ci.yml)
31
+ [![](https://github.com/jiacai2050/zig-cc/actions/workflows/zig.yml/badge.svg)](https://github.com/jiacai2050/zig-cc/actions/workflows/zig.yml)
32
+ [![](https://github.com/jiacai2050/zig-cc/actions/workflows/release.yml/badge.svg)](https://github.com/jiacai2050/zig-cc/actions/workflows/release.yml)
33
+ [<https://img.shields.io/pypi/v/zigcc>](https://pypi.org/project/zigcc)
34
+
35
+ A util script that aims to ease use `zig cc`{.verbatim} to compile
36
+ C/C++/Rust/Go programs.
37
+
38
+ # Why
39
+
40
+ In most cases, we can use following command to use Zig for compile
41
+
42
+ ``` bash
43
+ CC='zig cc' CXX='zig c++' ...
44
+ ```
45
+
46
+ However in real world, there are many issues this way, such as:
47
+
48
+ - [zig cc: parse -target and -mcpu/-march/-mtune flags according to
49
+ clang #4911](https://github.com/ziglang/zig/issues/4911)
50
+ - [Targets compare: Rust to
51
+ Zig](https://gist.github.com/kassane/446889ea1dd5ff07048d921f2b755e78)
52
+ - [unsupported linker
53
+ arg](https://github.com/search?q=repo%3Aziglang%2Fzig+unsupported+linker+arg%3A&type=issues)
54
+ - [Rust + \`zig cc\` CRT conflict. :
55
+ rust](https://www.reddit.com/r/rust/comments/q866qx/rust_zig_cc_crt_conflict/)
56
+
57
+ So this project was born, it will
58
+
59
+ - Convert target between Zig and Rust/Go
60
+ - Ignore link args when `zig cc`{.verbatim} throw errors, hopefully
61
+ this will make compile successfully, WIP.
62
+
63
+ # Install
64
+
65
+ ``` bash
66
+ pip install -U zigcc
67
+ ```
68
+
69
+ This will install three executables:
70
+
71
+ - `zigcc`{.verbatim}, used for `CC`{.verbatim}
72
+ - `zigcxx`{.verbatim}, used for `CXX`{.verbatim}
73
+ - `zigcargo`{.verbatim} can used to replace `cargo`{.verbatim}, it
74
+ will automatically set
75
+ - `CC`{.verbatim} `CARGO_TARGET_<triple>_LINKER`{.verbatim} to
76
+ `zigcc`{.verbatim}
77
+ - `CXX`{.verbatim} to `zigcxx`{.verbatim}
78
+
79
+ # Use in GitHub Action
80
+
81
+ Adding a step to your workflow like this:
82
+
83
+ ``` yaml
84
+ - uses: jiacai2050/zigcc@main
85
+ with:
86
+ zig-version: master
87
+ ```
88
+
89
+ Then you can invoke `zigcc`{.verbatim} `zigcxx`{.verbatim}
90
+ `zigcargo`{.verbatim} in following steps.
91
+
92
+ # Config
93
+
94
+ There some are env variable to config zigcc:
95
+
96
+ - `ZIGCC_FLAGS`{.verbatim}, space separated flags, pass to zig cc. An
97
+ example is set this to `-fno-sanitize=undefined`{.verbatim} to
98
+ disable sanitize since they may break your programs. See [Catching
99
+ undefined behavior with zig
100
+ cc](https://nathancraddock.com/blog/zig-cc-undefined-behavior/)
101
+
102
+ - `ZIGCC_BLACKLIST_FLAGS`{.verbatim}, space separated flags, used to
103
+ filter flags `zig cc`{.verbatim} don\'t support, such as
104
+ `-Wl,-dylib`{.verbatim} otherwise you could see errors below
105
+
106
+ ``` bash
107
+ = note: error: unsupported linker arg: -dylib
108
+ ```
109
+
110
+ - `ZIGCC_VERBOSE`{.verbatim} Set to `1`{.verbatim} enable verbose
111
+ logs.
zigcc-0.1.0/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # zigcc
2
+
3
+ [![](https://github.com/jiacai2050/zig-cc/actions/workflows/ci.yml/badge.svg)](https://github.com/jiacai2050/zig-cc/actions/workflows/ci.yml)
4
+ [![](https://github.com/jiacai2050/zig-cc/actions/workflows/zig.yml/badge.svg)](https://github.com/jiacai2050/zig-cc/actions/workflows/zig.yml)
5
+ [![](https://github.com/jiacai2050/zig-cc/actions/workflows/release.yml/badge.svg)](https://github.com/jiacai2050/zig-cc/actions/workflows/release.yml)
6
+ [<https://img.shields.io/pypi/v/zigcc>](https://pypi.org/project/zigcc)
7
+
8
+ A util script that aims to ease use `zig cc`{.verbatim} to compile
9
+ C/C++/Rust/Go programs.
10
+
11
+ # Why
12
+
13
+ In most cases, we can use following command to use Zig for compile
14
+
15
+ ``` bash
16
+ CC='zig cc' CXX='zig c++' ...
17
+ ```
18
+
19
+ However in real world, there are many issues this way, such as:
20
+
21
+ - [zig cc: parse -target and -mcpu/-march/-mtune flags according to
22
+ clang #4911](https://github.com/ziglang/zig/issues/4911)
23
+ - [Targets compare: Rust to
24
+ Zig](https://gist.github.com/kassane/446889ea1dd5ff07048d921f2b755e78)
25
+ - [unsupported linker
26
+ arg](https://github.com/search?q=repo%3Aziglang%2Fzig+unsupported+linker+arg%3A&type=issues)
27
+ - [Rust + \`zig cc\` CRT conflict. :
28
+ rust](https://www.reddit.com/r/rust/comments/q866qx/rust_zig_cc_crt_conflict/)
29
+
30
+ So this project was born, it will
31
+
32
+ - Convert target between Zig and Rust/Go
33
+ - Ignore link args when `zig cc`{.verbatim} throw errors, hopefully
34
+ this will make compile successfully, WIP.
35
+
36
+ # Install
37
+
38
+ ``` bash
39
+ pip install -U zigcc
40
+ ```
41
+
42
+ This will install three executables:
43
+
44
+ - `zigcc`{.verbatim}, used for `CC`{.verbatim}
45
+ - `zigcxx`{.verbatim}, used for `CXX`{.verbatim}
46
+ - `zigcargo`{.verbatim} can used to replace `cargo`{.verbatim}, it
47
+ will automatically set
48
+ - `CC`{.verbatim} `CARGO_TARGET_<triple>_LINKER`{.verbatim} to
49
+ `zigcc`{.verbatim}
50
+ - `CXX`{.verbatim} to `zigcxx`{.verbatim}
51
+
52
+ # Use in GitHub Action
53
+
54
+ Adding a step to your workflow like this:
55
+
56
+ ``` yaml
57
+ - uses: jiacai2050/zigcc@main
58
+ with:
59
+ zig-version: master
60
+ ```
61
+
62
+ Then you can invoke `zigcc`{.verbatim} `zigcxx`{.verbatim}
63
+ `zigcargo`{.verbatim} in following steps.
64
+
65
+ # Config
66
+
67
+ There some are env variable to config zigcc:
68
+
69
+ - `ZIGCC_FLAGS`{.verbatim}, space separated flags, pass to zig cc. An
70
+ example is set this to `-fno-sanitize=undefined`{.verbatim} to
71
+ disable sanitize since they may break your programs. See [Catching
72
+ undefined behavior with zig
73
+ cc](https://nathancraddock.com/blog/zig-cc-undefined-behavior/)
74
+
75
+ - `ZIGCC_BLACKLIST_FLAGS`{.verbatim}, space separated flags, used to
76
+ filter flags `zig cc`{.verbatim} don\'t support, such as
77
+ `-Wl,-dylib`{.verbatim} otherwise you could see errors below
78
+
79
+ ``` bash
80
+ = note: error: unsupported linker arg: -dylib
81
+ ```
82
+
83
+ - `ZIGCC_VERBOSE`{.verbatim} Set to `1`{.verbatim} enable verbose
84
+ logs.
@@ -0,0 +1,61 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "zigcc"
7
+ dynamic = ["version"]
8
+ authors = [{name = "Jiacai Liu", email="dev@liujiacai.net"}]
9
+ description = "A util script that aims to ease use `zig cc` to compile C/C++/Rust/Go programs."
10
+ readme = "README.md"
11
+ keywords = ["zig", "compile", "rust", "cross-compile"]
12
+ license = "GPL-3.0"
13
+ requires-python = ">=3.0.0"
14
+ dependencies = [
15
+ ]
16
+ classifiers = [
17
+ "Operating System :: OS Independent",
18
+ "Topic :: Software Development",
19
+ "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
20
+ "Intended Audience :: Information Technology",
21
+ "Intended Audience :: System Administrators",
22
+ "Intended Audience :: Developers",
23
+ "Programming Language :: Python :: 3",
24
+ "Programming Language :: Python :: 3 :: Only",
25
+ "Programming Language :: Python :: 3.6",
26
+ "Programming Language :: Python :: 3.7",
27
+ "Programming Language :: Python :: 3.8",
28
+ "Programming Language :: Python :: 3.9",
29
+ "Programming Language :: Python :: 3.10",
30
+ "Programming Language :: Python :: 3.11",
31
+ ]
32
+
33
+ [project.urls]
34
+ Repository = "https://github.com/jiacai2050/zigcc"
35
+ Issues = "https://github.com/jiacai2050/zigcc/issues"
36
+
37
+ [project.scripts]
38
+ zigcc = "zigcc:main"
39
+ zigcxx = "zigcc:main"
40
+ zigcargo = "zigcc:main"
41
+
42
+ [tool.hatch.version]
43
+ path = "zigcc/__init__.py"
44
+
45
+ [tool.hatch.build.targets.sdist]
46
+ include = [
47
+ "zigcc",
48
+ "pyproject.toml",
49
+ ]
50
+
51
+ [tool.hatch.build.targets.wheel]
52
+ include = [
53
+ "zigcc"
54
+ ]
55
+
56
+ # https://github.com/astral-sh/ruff?tab=readme-ov-file#configuration
57
+ [tool.ruff.lint]
58
+ fixable = ["ALL"]
59
+
60
+ [tool.ruff.format]
61
+ quote-style = "single"
@@ -0,0 +1,189 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # A util script that aims to ease use `zig cc` to compile C/C++/Rust/Go programs.
4
+ #
5
+ # How it works:
6
+ # This script will infer which command to run based on `sys.argv[0]`, which is
7
+ # the program name, currently it supports:
8
+ # 1. zigcc, invoke `zig cc`
9
+ # 2. zigcxx, invoke `zig c++`
10
+ # 3. zigcargo, invoke `cargo` with `CC`, `CXX`, and rustc linker set to zig.
11
+ #
12
+ # Supported env vars:
13
+ # 1. `ZIGCC_VERBOSE`, enable verbose log
14
+ # 2. `ZIGCC_FLAGS`, extra flags passed to zig, such as `-fno-sanitize=undefined`
15
+
16
+ import sys
17
+ import os
18
+ import logging
19
+ import subprocess
20
+ import platform
21
+
22
+ __VERSION__ = '0.1.0'
23
+
24
+ UNKNOWN = 0
25
+ RUST = 1
26
+ GO = 2
27
+ IS_MACOS = platform.system() == 'Darwin'
28
+ ENABLE_LOG = os.getenv('ZIGCC_VERBOSE', '0') == '1'
29
+ FLAGS = os.getenv('ZIGCC_FLAGS', '').split(' ')
30
+ FLAGS = [f for f in FLAGS if f != '']
31
+
32
+ # Blacklist flags, wild match
33
+ BLACKLIST_WILD_FLAGS = os.getenv('ZIGCC_BLACKLIST_FLAGS', '').split(' ') + [
34
+ '--target',
35
+ '-exported_symbols_list',
36
+ '-no_pie',
37
+ '-Wl,-dylib',
38
+ # https://github.com/ziglang/zig/issues/5320
39
+ 'self-contained/rcrt1.o',
40
+ 'self-contained/crti.o',
41
+ ]
42
+ BLACKLIST_WILD_FLAGS = [f for f in BLACKLIST_WILD_FLAGS if f != '']
43
+
44
+
45
+ def log(msg, *args, **kwargs):
46
+ if ENABLE_LOG:
47
+ logging.info(msg, *args, **kwargs)
48
+
49
+
50
+ def zig_target_from(target, lang):
51
+ if lang == RUST:
52
+ # Zig target has no vendor field
53
+ # i686-pc-windows-msvc --> x86-windows-msvc
54
+ triple = target.split('-')
55
+ if len(triple) == 4:
56
+ [arch, vendor, os, abi] = triple
57
+ # aarch64-apple-darwin
58
+ elif len(triple) == 3:
59
+ [arch, vendor, os] = triple
60
+ abi = 'none'
61
+ else:
62
+ return target
63
+
64
+ zig_arch = {
65
+ 'i686': 'x86',
66
+ }.get(arch, arch)
67
+ zig_os = {'darwin': 'macos'}.get(os, os)
68
+ return '-'.join([zig_arch, zig_os, abi])
69
+ elif lang == GO:
70
+ [arch, os] = target.split('-', 2)
71
+ zig_arch = {
72
+ '386': 'x86',
73
+ 'amd64': 'x86_64',
74
+ 'arm64': 'aarch64',
75
+ }.get(arch, arch)
76
+ zig_os = {'darwin': 'macos'}.get(os, os)
77
+ return '-'.join([zig_arch, zig_os])
78
+ else:
79
+ return target
80
+
81
+
82
+ # Detect zig target from language specific vars.
83
+ def detect_zig_target():
84
+ target = os.getenv('CARGO_BUILD_TARGET')
85
+ if target is not None:
86
+ return zig_target_from(target, RUST)
87
+
88
+ goos = os.getenv('GOOS')
89
+ if goos is not None:
90
+ goarch = os.getenv('GOARCH')
91
+ return zig_target_from('{}-{}'.format(goarch, goos), GO)
92
+
93
+ return None
94
+
95
+
96
+ def cargo_linker_var_name(target):
97
+ return 'CARGO_TARGET_{}_LINKER'.format(target.replace('-', '_').upper())
98
+
99
+
100
+ def guess_rust_target(args):
101
+ found_target = False
102
+ for arg in args:
103
+ if arg == '--target':
104
+ found_target = True
105
+ continue
106
+ if found_target:
107
+ return arg
108
+
109
+ target = os.getenv('CARGO_BUILD_TARGET')
110
+ if target is not None:
111
+ return target
112
+
113
+ try:
114
+ stdout = subprocess.check_output(['rustc', '-Vv']).decode('utf8')
115
+ for line in stdout.split('\n'):
116
+ kv = line.split(':', 2)
117
+ if len(kv) > 1:
118
+ [key, value] = kv
119
+ if key == 'host':
120
+ return value.strip()
121
+ except Exception as e:
122
+ log('Get rustc triple failed, err:{}', e)
123
+ raise e
124
+
125
+
126
+ def run_subprocess(args, env):
127
+ log('Begin run command\nArgs:%s\nEnv:%s', args, env)
128
+ try:
129
+ subprocess.run(args, check=True, env=env)
130
+ except subprocess.CalledProcessError as e:
131
+ log(f'Command {e.cmd} failed with error {e.returncode}')
132
+ sys.exit(e.returncode)
133
+
134
+
135
+ def run_as_cargo(args):
136
+ target = guess_rust_target(args)
137
+ env = os.environ.copy()
138
+ env[cargo_linker_var_name(target)] = 'zigcc'
139
+ env['CC'] = 'zigcc'
140
+ env['CXX'] = 'zigcxx'
141
+ run_subprocess(['cargo'] + args, env)
142
+
143
+
144
+ def main():
145
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
146
+ program = os.path.basename(sys.argv[0])
147
+ args = sys.argv[1:]
148
+ if program == 'zigcargo':
149
+ run_as_cargo(args)
150
+ sys.exit(0)
151
+
152
+ run_args = {
153
+ 'zigcc': ['zig', 'cc'],
154
+ 'zigcxx': ['zig', 'c++'],
155
+ }.get(program)
156
+ if run_args is None:
157
+ print(f'Unknown program, {program}')
158
+ sys.exit(1)
159
+
160
+ target = detect_zig_target()
161
+ if target is not None:
162
+ run_args += ['-target', target]
163
+
164
+ for flag in FLAGS:
165
+ run_args.append(flag)
166
+
167
+ # https://github.com/ziglang/zig/issues/10299#issuecomment-989736808
168
+ # Append $(xcrun --show-sdk-path)/System/Library/Frameworks in search path
169
+ if IS_MACOS:
170
+ root_path = subprocess.getoutput('xcrun --show-sdk-path')
171
+ run_args += [f'-F{root_path}/System/Library/Frameworks']
172
+
173
+ for arg in args:
174
+ found = False
175
+ for wild_args in BLACKLIST_WILD_FLAGS:
176
+ if wild_args in arg:
177
+ found = True
178
+ break
179
+
180
+ if found:
181
+ continue
182
+
183
+ run_args.append(arg)
184
+
185
+ run_subprocess(run_args, os.environ)
186
+
187
+
188
+ if __name__ == '__main__':
189
+ main()