Skip to content

Commit 434412a

Browse files
committed
Improve setup script
1 parent 16572f4 commit 434412a

6 files changed

Lines changed: 74 additions & 52 deletions

File tree

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/.vscode/*
2-
!/.vscode/tasks.json
1+
/.vscode
32
**/__pycache__
43
**/.mypy_cache

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
Cross files of gcc-arm-none-eabi and ARMClang for [Meson build system](https://mesonbuild.com).
44

55
## Usage
6-
1. Get python wheel from (release page)(https://github.com/JalonWong/mcu_meson/releases)
7-
2. `pip install ./mcu_meson_setup-xxx.whl`
6+
1. Get python wheel from [release page](https://github.com/JalonWong/mcu_meson/releases)
7+
2. `pip install ./mcu_meson_setup-*.whl`
88
3. Copy [example/setup.py](example/setup.py) to your project, and modify it.
9+
1. the link of cross files can be a:
10+
- URL link: `https://*`
11+
- local relative path: `../*`
12+
- main branch of this repository: `main:gcc-arm-none-eabi.ini`
13+
- tag of this repository: `tag:v0.3.0:gcc-arm-none-eabi.ini`
914
4. Run `python setup.py`
10-
1. You can see options via `python setup.py --h`
11-
5. Use meson
15+
1. You can see options via `python setup.py --h`
16+
2. The extra arguments will be passed to meson `python setup.py --debug`
17+
3. You can also pass arguments via the function `setup(args=["--debug"])`
1218

1319
See also [example](example).
1420

example/.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
/build
1+
/.vscode/*
2+
!/.vscode/tasks.json
3+
24
/builddir*
35
/subprojects/*
46
!/subprojects/*.wrap
5-
/.vscode/*
6-
!/.vscode/tasks.json
7-
/cross_files

example/setup.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from mcu_meson_setup import setup
22

3+
setup("builddir-native", vsenv=True)
34
setup(
45
"builddir",
5-
[
6-
"https://raw.githubusercontent.com/JalonWong/mcu_meson/refs/heads/main/gcc-arm-none-eabi.ini",
7-
"https://raw.githubusercontent.com/JalonWong/mcu_meson/refs/heads/main/gcc-cortex-m3.ini",
8-
],
6+
["main:gcc-arm-none-eabi.ini", "main:gcc-cortex-m3.ini"],
97
link_script="src/my_link.ld",
108
output_map="app.map",
119
)
@@ -14,13 +12,11 @@
1412
# ["../gcc-arm-none-eabi.ini", "../gcc-cortex-m3.ini"],
1513
# link_script="src/my_link.ld",
1614
# output_map="app.map",
15+
# wipe=True,
1716
# )
1817
# setup(
1918
# "builddir",
20-
# [
21-
# "https://raw.githubusercontent.com/JalonWong/mcu_meson/refs/heads/main/armclang.ini",
22-
# "https://raw.githubusercontent.com/JalonWong/mcu_meson/refs/heads/main/clang-cortex-m3.ini",
23-
# ],
19+
# ["main:armclang.ini", "tag:v0.3.0:clang-cortex-m3.ini"],
2420
# link_script="src/my_link.sct",
2521
# output_map="app.map",
2622
# )

mcu_meson_setup/mcu_meson_setup.py

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ def find_path(cmd: str) -> Path | None:
3838

3939

4040
def modify_cross_file(file: Path, link_script: str, output_map: str, arm_path: str | None) -> None:
41-
if str(file).endswith("gcc-arm-none-eabi.ini"):
42-
arm_cmd = "arm-none-eabi-gcc"
43-
else:
41+
print(green("Modifying:"), file.as_posix(), flush=True)
42+
if str(file).endswith("armclang.ini"):
4443
arm_cmd = "armclang"
44+
else:
45+
arm_cmd = "arm-none-eabi-gcc"
4546

4647
if arm_path:
4748
path = Path(arm_path)
@@ -92,51 +93,72 @@ def modify_cross_file(file: Path, link_script: str, output_map: str, arm_path: s
9293
file.write_text(text)
9394

9495

95-
def download_files(cross_files: list[str]) -> list[str]:
96-
path = Path("cross_files")
97-
if not path.exists():
98-
os.mkdir(path)
96+
def download_file(url: str, file: Path) -> None:
97+
print(green("Downloading:"), f"{url} -> {file.as_posix()}", flush=True)
98+
urllib.request.urlretrieve(url, file)
99+
100+
101+
def prepare_cross_files(build_dir: Path, cross_files: list[str]) -> list[str]:
102+
cross_dir = build_dir.joinpath("cross_files")
103+
os.makedirs(cross_dir, exist_ok=True)
104+
105+
repo = "https://raw.githubusercontent.com/JalonWong/mcu_meson"
99106

100107
rst_list = []
101108
for f in cross_files:
102-
if f.startswith("http"):
103-
filename = path.joinpath(f.rsplit("/", maxsplit=1)[1])
104-
urllib.request.urlretrieve(f, filename)
105-
rst_list.append(str(filename))
109+
if f.startswith("main:"):
110+
filename = cross_dir.joinpath(f.rsplit(":", maxsplit=1)[1])
111+
download_file(
112+
f.replace("main:", f"{repo}/refs/heads/main/"),
113+
filename,
114+
)
115+
elif f.startswith("tag:"):
116+
tag_list = f.split(":")
117+
filename = cross_dir.joinpath(tag_list[2])
118+
download_file(
119+
f"{repo}/{tag_list[1]}/{tag_list[2]}",
120+
filename,
121+
)
122+
elif f.startswith("http"):
123+
filename = cross_dir.joinpath(f.rsplit("/", maxsplit=1)[1])
124+
download_file(f, filename)
106125
else:
107-
rst_list.append(f)
126+
filename = cross_dir.joinpath(Path(f).name)
127+
print(green("Copying:"), f"{f} -> {filename.as_posix()}", flush=True)
128+
shutil.copy(f, filename)
129+
130+
rst_list.append(filename.as_posix())
108131

109132
return rst_list
110133

111134

112135
def setup(
113136
build_dir: str,
114-
cross_files: list[str],
137+
cross_files: list[str] = [],
115138
link_script: str = "",
116139
output_map: str = "",
117-
msvc: bool = False,
140+
reconfigure: bool = True,
141+
wipe: bool = False,
142+
vsenv: bool = False,
143+
args: list[str] = [],
118144
) -> None:
119145
parser = argparse.ArgumentParser()
120-
parser.add_argument("--rm", help="Remove builddir before setup", action="store_true")
121-
parser.add_argument("--native", help="Setup for native at the same time", action="store_true")
122146
parser.add_argument("--arm_path", help="Path of arm toolchain", type=str)
123-
opts = parser.parse_args()
124-
125-
# Native
126-
if opts.native:
127-
if opts.rm and os.path.exists(f"{build_dir}-native"):
128-
shutil.rmtree(f"{build_dir}-native")
129-
cmd = f"meson setup {build_dir}-native".split()
130-
if msvc and platform.system() == "Windows":
131-
cmd += ["--vsenv"]
132-
print(green("Run:"), " ".join(cmd), flush=True)
133-
subprocess.run(cmd)
134-
135-
# Cross
136-
if opts.rm and os.path.exists(build_dir):
147+
opts, extra = parser.parse_known_args()
148+
149+
if wipe and os.path.exists(build_dir):
150+
print(green("Wipe:"), f"Removing {build_dir}", flush=True)
137151
shutil.rmtree(build_dir)
138-
cross_files = download_files(cross_files)
139-
modify_cross_file(Path(cross_files[0]), link_script, output_map, opts.arm_path)
152+
153+
cross_files = prepare_cross_files(Path(build_dir), cross_files)
154+
if len(cross_files) > 0:
155+
modify_cross_file(Path(cross_files[0]), link_script, output_map, opts.arm_path)
156+
140157
cmd = f"meson setup {build_dir}".split() + [f"--cross-file={f}" for f in cross_files]
141-
print(green("Run:"), " ".join(cmd), flush=True)
158+
if reconfigure:
159+
cmd += ["--reconfigure"]
160+
if vsenv and platform.system() == "Windows":
161+
cmd += ["--vsenv"]
162+
163+
print(green("Running:"), " ".join(cmd + args + extra), flush=True)
142164
subprocess.run(cmd)

mcu_meson_setup/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
description = "Help you get and setup cross files for meson"
44
name = "mcu-meson-setup"
55
requires-python = ">=3.10"
6-
version = "0.2.1"
6+
version = "0.3.0"

0 commit comments

Comments
 (0)