-
Notifications
You must be signed in to change notification settings - Fork 10
[com9] Add com9 compilation (instead of fixed .exe) #1281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fso42
wants to merge
12
commits into
master
Choose a base branch
from
addCom9Compil
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
18c38ba
com9MoTVoellmy: build MoT-Voellmy binaries at package time
fso42 7200b00
com9MoTVoellmy: improve gcc-not-found error with download hint
fso42 4c10985
com9MoTVoellmy: drop env var source option, document build process
fso42 cc47fe8
fix(com9MoTVoellmy): update `simTypeList` to use all available input …
fso42 85fd6e9
test(com9MoTVoellmy): add integration test with avaKot
fso42 ae18548
chore(coverage): exclude com9MoTVoellmy build script
fso42 76c559f
feat(com9MoTVoellmy): enable macOS runtime, add universal binary flags
fso42 dfd5731
chore(github-actions): update `cibuildwheel` paths for `com9MoTVoellm…
fso42 86146f3
chore(github-actions): add `CIBW_ENVIRONMENT` variable to workflows
fso42 c31c812
fix(com9MoTVoellmy): remove `-static` flag from Linux build command
fso42 b08c52c
fix(com9MoTVoellmy): update macOS task to use platform-specific binary
fso42 bfafb08
fix(com9MoTVoellmy): pin C source download to latest GitHub release
fso42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ omit = */test_* | |
| */__init__.py | ||
| */run* | ||
| */out3Plot/out* | ||
| */com9MoTVoellmy/_buildMoTVoellmy.py | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| """Build MoT-Voellmy binary for the current platform. | ||
|
|
||
| Usage: | ||
| python _buildMoTVoellmy.py [path/to/source.c] | ||
|
|
||
| If a path is provided, that C source file is used. Otherwise, the script | ||
| discovers and downloads the latest source from the upstream GitHub repo. | ||
|
|
||
| On success, the compiled binary is written to this directory alongside the script. | ||
| On failure, the script exits with a non-zero code (for pixi task / CI). | ||
| When imported from setup.py, it returns a status instead of exiting. | ||
| """ | ||
|
|
||
| import os | ||
| import platform | ||
| import re | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import urllib.request | ||
|
|
||
| upstreamRepo = "norwegian-geotechnical-institute/MoT-Voellmy" | ||
| upstreamApi = f"https://api.github.com/repos/{upstreamRepo}" | ||
| upstreamContents = f"{upstreamApi}/contents/" | ||
| sourcePattern = re.compile(r"^MoT-Voellmy\..*\.c$") | ||
|
|
||
| outputDir = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
|
|
||
| def _getReleaseTag(): | ||
| """Return the tag name of the latest GitHub release, or None.""" | ||
| headers = {} | ||
| token = os.environ.get("GITHUB_TOKEN") | ||
| if token: | ||
| headers["Authorization"] = f"Bearer {token}" | ||
|
|
||
| try: | ||
| url = f"{upstreamApi}/releases/latest" | ||
| req = urllib.request.Request(url, headers=headers) | ||
| with urllib.request.urlopen(req, timeout=30) as resp: | ||
| import json | ||
|
|
||
| data = json.loads(resp.read().decode()) | ||
| tag = data.get("tag_name") | ||
| if tag: | ||
| print(f"Latest upstream release: {tag}") | ||
| return tag | ||
| except Exception as e: | ||
| print(f"Failed to query GitHub releases: {e}", file=sys.stderr) | ||
| return None | ||
|
|
||
|
|
||
| def _findGithubSource(): | ||
| """Query the latest GitHub release for the .c source file. | ||
|
|
||
| Returns (download_url, filename) or None if not found. | ||
| """ | ||
| headers = {} | ||
| token = os.environ.get("GITHUB_TOKEN") | ||
| if token: | ||
| headers["Authorization"] = f"Bearer {token}" | ||
|
|
||
| tag = _getReleaseTag() | ||
| ref = tag if tag else "main" | ||
|
|
||
| try: | ||
| url = f"{upstreamContents}?ref={ref}" | ||
| req = urllib.request.Request(url, headers=headers) | ||
| with urllib.request.urlopen(req, timeout=30) as resp: | ||
| import json | ||
|
|
||
| contents = json.loads(resp.read().decode()) | ||
| except Exception as e: | ||
| print(f"Failed to query GitHub API: {e}", file=sys.stderr) | ||
| return None | ||
|
|
||
| for item in contents: | ||
| if item.get("type") != "file": | ||
| continue | ||
| name = item.get("name", "") | ||
| if sourcePattern.match(name): | ||
| url = item.get("download_url") | ||
| if url: | ||
| print(f"Found upstream source: {name}") | ||
| return url, name | ||
|
|
||
| print("No MoT-Voellmy source file found in upstream repo", file=sys.stderr) | ||
| return None | ||
|
|
||
|
|
||
| def _downloadSource(url, destPath): | ||
| """Download a file from url to destPath.""" | ||
| print(f"Downloading {url} ...") | ||
| try: | ||
| with urllib.request.urlopen(url, timeout=60) as resp: | ||
| with open(destPath, "wb") as f: | ||
| shutil.copyfileobj(resp, f) | ||
| print(f"Downloaded to {destPath}") | ||
| return True | ||
| except Exception as e: | ||
| print(f"Download failed: {e}", file=sys.stderr) | ||
| return False | ||
|
|
||
|
|
||
| def _compile(sourcePath): | ||
| """Compile sourcePath for the current platform. | ||
|
|
||
| Returns True on success, False on failure. | ||
| """ | ||
| system = platform.system() | ||
|
|
||
| if system == "Linux": | ||
| outName = "MoT-Voellmy_linux.exe" | ||
| cmd = ["gcc", "-Wall", "-pedantic", "-o", outName, sourcePath, "-lm"] | ||
| elif system == "Windows": | ||
| outName = "MoT-Voellmy_win.exe" | ||
| cmd = ["gcc", "-Wall", "-pedantic", "-o", outName, sourcePath, "-lm"] | ||
| elif system == "Darwin": | ||
| outName = "MoT-Voellmy_mac.exe" | ||
| cmd = ["gcc", "-Wall", "-pedantic", "-arch", "arm64", "-arch", "x86_64", | ||
| "-o", outName, sourcePath, "-lm"] | ||
| else: | ||
| print(f"Unknown platform: {system}", file=sys.stderr) | ||
| return False | ||
|
|
||
| # Run from outputDir so the binary lands alongside the Python module | ||
| print(f"Compiling: {' '.join(cmd)}") | ||
| try: | ||
| result = subprocess.run(cmd, cwd=outputDir, capture_output=True, text=True) | ||
| if result.returncode != 0: | ||
| print(f"Compilation failed:\n{result.stderr}", file=sys.stderr) | ||
| return False | ||
| except FileNotFoundError: | ||
| print( | ||
| f"gcc not found. Install gcc (e.g. MinGW on Windows) or download " | ||
| f"the precompiled binary from https://github.com/norwegian-geotechnical-institute/" | ||
| f"MoT-Voellmy and copy it to the com9MoTVoellmy directory as {outName}.", | ||
| file=sys.stderr, | ||
| ) | ||
| return False | ||
|
|
||
| # Make executable on Unix | ||
| outPath = os.path.join(outputDir, outName) | ||
| if system != "Windows": | ||
| os.chmod(outPath, 0o755) | ||
|
|
||
| print(f"Compiled {outPath}") | ||
|
fso42 marked this conversation as resolved.
fso42 marked this conversation as resolved.
qltysh[bot] marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return True | ||
|
|
||
|
|
||
| def buildMoTVoellmy(sourcePath=None): | ||
| """Compile MoT-Voellmy binary. | ||
|
|
||
| Args: | ||
| sourcePath: Optional path to a local .c file. If None, downloads | ||
| the latest source from the upstream GitHub repo. | ||
|
|
||
| Returns: | ||
| True if compilation succeeded, False otherwise. | ||
| """ | ||
| # 1. CLI argument | ||
| if sourcePath is not None: | ||
| if not os.path.isfile(sourcePath): | ||
| print(f"Source file not found: {sourcePath}", file=sys.stderr) | ||
| return False | ||
| return _compile(sourcePath) | ||
|
|
||
| # 2. GitHub download | ||
| result = _findGithubSource() | ||
| if result is None: | ||
| return False | ||
|
|
||
| url, filename = result | ||
| dest = os.path.join(outputDir, filename) | ||
|
|
||
| # Use cached copy if available and download is a fallback | ||
| if not os.path.isfile(dest): | ||
| if not _downloadSource(url, dest): | ||
| return False | ||
|
|
||
|
fso42 marked this conversation as resolved.
|
||
| return _compile(dest) | ||
|
|
||
|
|
||
| def main(): | ||
| source = sys.argv[1] if len(sys.argv) > 1 else None | ||
| success = buildMoTVoellmy(source) | ||
| sys.exit(0 if success else 1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.