Skip to content

Commit

Permalink
Add build files
Browse files Browse the repository at this point in the history
  • Loading branch information
aivora-beamng committed May 14, 2024
1 parent 2b3f329 commit adcff5f
Show file tree
Hide file tree
Showing 11 changed files with 709 additions and 7 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- run: python3 build/lint_files.py
- run: python3 dist/lint_files.py

build:
strategy:
Expand Down Expand Up @@ -42,14 +42,14 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install gcc-aarch64-linux-gnu qemu-user
- run: python build/build_cvode.py ${{ matrix.name }}
- run: python build/build_libxml2.py ${{ matrix.name }}
- run: python build/build_zlib.py ${{ matrix.name }}
- run: python build/build.py ${{ matrix.name }}
- run: python dist/build_cvode.py ${{ matrix.name }}
- run: python dist/build_libxml2.py ${{ matrix.name }}
- run: python dist/build_zlib.py ${{ matrix.name }}
- run: python dist/build.py ${{ matrix.name }}
- uses: actions/upload-artifact@v3
with:
name: ${{ matrix.name }}
path: build/fmus
path: dist/fmus
if-no-files-found: error

merge-fmus:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ __pycache__
/venv/
tests/work/
.vscode
build
build
dist
8 changes: 8 additions & 0 deletions dist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.tar.gz
cvode-*/
libxml2-*/
zlib-*/
fmi1-*/
fmi2-*/
fmi3-*/
fmus/
3 changes: 3 additions & 0 deletions dist/aarch64-linux-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
110 changes: 110 additions & 0 deletions dist/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# build FMUs and fmusim for all FMI versions

import os
import shutil
import subprocess
from pathlib import Path
import argparse


parent_dir = Path(__file__).parent

parser = argparse.ArgumentParser()
parser.add_argument(
'platform',
choices={'x86-windows', 'x86_64-windows', 'x86_64-linux', 'aarch64-linux', 'x86_64-darwin', 'aarch64-darwin'},
help="Platform to build for, e.g. x86_64-windows"
)
parser.add_argument('--cmake-generator')
args, _ = parser.parse_known_args()


def build_fmus(fmi_version, fmi_type=None):

if fmi_type is not None:
build_dir = parent_dir / f'fmi{fmi_version}-{fmi_type}-{args.platform}'
else:
build_dir = parent_dir / f'fmi{fmi_version}-{args.platform}'

if build_dir.exists():
shutil.rmtree(build_dir)

os.makedirs(build_dir)

cmake_args = []

fmi_platform = args.platform
fmi_architecture, fmi_system = fmi_platform.split('-')

if fmi_system == 'windows':

cmake_generator = 'Visual Studio 17 2022' if args.cmake_generator is None else args.cmake_generator

if fmi_architecture == 'x86':
cmake_args += ['-G', cmake_generator, '-A', 'Win32']
elif fmi_architecture == 'x86_64':
cmake_args += ['-G', cmake_generator, '-A', 'x64']

elif fmi_platform == 'aarch64-linux':

toolchain_file = parent_dir / 'aarch64-linux-toolchain.cmake'
cmake_args += ['-D', f'CMAKE_TOOLCHAIN_FILE={ toolchain_file }']

elif fmi_platform == 'x86_64-darwin':

cmake_args += ['-D', 'CMAKE_OSX_ARCHITECTURES=x86_64']

elif fmi_platform == 'aarch64-darwin':

cmake_args += ['-D', 'CMAKE_OSX_ARCHITECTURES=arm64']

install_dir = build_dir / 'install'

if fmi_type is not None:
cmake_args += ['-D', f'FMI_TYPE={fmi_type.upper()}']

cmake_args += [
'-D', f'CMAKE_INSTALL_PREFIX={install_dir}',
'-D', f'FMI_VERSION={fmi_version}',
'-D', f'FMI_ARCHITECTURE={fmi_architecture}',
'-D', 'WITH_FMUSIM=ON',
'-B', build_dir,
parent_dir.parent
]

subprocess.check_call(['cmake'] + cmake_args)
subprocess.check_call(['cmake', '--build', build_dir, '--target', 'install', '--config', 'Release'])

fmus_dir = parent_dir / 'fmus' / f'{fmi_version}.0'

if fmi_type is not None:
fmus_dir = fmus_dir / fmi_type

if fmus_dir.exists():
shutil.rmtree(fmus_dir)

os.makedirs(fmus_dir)

fmusim_dir = parent_dir / 'fmus' / f'fmusim-{args.platform}'

if fmusim_dir.exists():
shutil.rmtree(fmusim_dir)

os.makedirs(fmusim_dir)

for root, dirs, files in os.walk(install_dir):
for file in files:
if file.endswith('.fmu'):
shutil.copyfile(src=install_dir / file, dst=fmus_dir / file)
elif file.startswith('fmusim'):
shutil.copyfile(src=install_dir / file, dst=fmusim_dir / file)


if __name__ == '__main__':

if args.platform in {'x86_64-linux', 'x86-windows', 'x86_64-windows', 'x86_64-darwin'}:
# build_fmus(fmi_version=1, fmi_type='me')
# build_fmus(fmi_version=1, fmi_type='cs')
build_fmus(fmi_version=2)

build_fmus(fmi_version=3)
76 changes: 76 additions & 0 deletions dist/build_cvode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from pathlib import Path
from subprocess import check_call
from fmpy.util import download_file
import tarfile
import argparse


parser = argparse.ArgumentParser()
parser.add_argument(
'platform',
choices={'x86-windows', 'x86_64-windows', 'x86_64-linux', 'aarch64-linux', 'x86_64-darwin', 'aarch64-darwin'},
help="Platform to build for, e.g. x86_64-windows"
)
args, _ = parser.parse_known_args()

archive = download_file('https://github.com/LLNL/sundials/releases/download/v6.4.1/cvode-6.4.1.tar.gz',
checksum='0a614e7d7d525d9ec88d5a30c887786d7c3681bd123bb6679fb9a4ec5f4609fe')

root = Path(__file__).parent

with tarfile.open(archive) as file:
file.extractall(root)

build_dir = root / f'cvode-{args.platform}' / 'build'

install_prefix = root / f'cvode-{args.platform}' / 'install'

cmake_args = []

fmi_platform = args.platform
fmi_architecture, fmi_system = fmi_platform.split('-')

if fmi_system == 'windows':

cmake_args = [
'-G', 'Visual Studio 17 2022',
'-D', 'CMAKE_C_FLAGS_RELEASE=/MT /O2 /Ob2 /DNDEBUG',
'-D', 'CMAKE_C_FLAGS_DEBUG=/MT /Zi /Ob0 /Od /RTC1',
'-A'
]

if fmi_architecture == 'x86':
cmake_args.append('Win32')
elif fmi_architecture == 'x86_64':
cmake_args.append('x64')

elif fmi_platform == 'aarch64-linux':

toolchain_file = root / 'aarch64-linux-toolchain.cmake'
cmake_args += ['-D', f'CMAKE_TOOLCHAIN_FILE={toolchain_file}']

elif fmi_platform == 'x86_64-darwin':

cmake_args += ['-D', 'CMAKE_OSX_ARCHITECTURES=x86_64']

elif fmi_platform == 'aarch64-darwin':

cmake_args += ['-D', 'CMAKE_OSX_ARCHITECTURES=arm64']

check_call(
['cmake'] +
cmake_args +
['-B', build_dir,
'-D', f'BUILD_SHARED_LIBS=OFF',
'-D', f'BUILD_TESTING=OFF',
'-D', f'EXAMPLES_INSTALL=OFF',
'-D', f'CMAKE_INSTALL_PREFIX={ install_prefix }',
root / 'cvode-6.4.1']
)

check_call([
'cmake',
'--build', build_dir,
'--config', 'Release',
'--target', 'install'
])
77 changes: 77 additions & 0 deletions dist/build_libxml2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from pathlib import Path
from subprocess import check_call
from fmpy.util import download_file
from fmpy import extract
import argparse


parser = argparse.ArgumentParser()
parser.add_argument(
'platform',
choices={'x86-windows', 'x86_64-windows', 'x86_64-linux', 'aarch64-linux', 'x86_64-darwin', 'aarch64-darwin'},
help="Platform to build for, e.g. x86_64-windows"
)
(args, _) = parser.parse_known_args()

archive = download_file('https://github.com/GNOME/libxml2/archive/refs/tags/v2.11.5.zip',
checksum='711675470075cc85ba450f56aff7424f1ecdef00bc5d1d5dced3ffecd1a9b772')

root = Path(__file__).parent

extract(archive, root)

build_dir = root / f'libxml2-{args.platform}' / 'build'

install_prefix = root / f'libxml2-{args.platform}' / 'install'

cmake_args = []

fmi_platform = args.platform
fmi_architecture, fmi_system = fmi_platform.split('-')

if fmi_system == 'windows':

cmake_args = [
'-G', 'Visual Studio 17 2022',
'-D', 'CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded',
'-A'
]

if fmi_architecture == 'x86':
cmake_args.append('Win32')
elif fmi_architecture == 'x86_64':
cmake_args.append('x64')

elif fmi_platform == 'aarch64-linux':

toolchain_file = root / 'aarch64-linux-toolchain.cmake'
cmake_args += ['-D', f'CMAKE_TOOLCHAIN_FILE={toolchain_file}']

elif fmi_platform == 'x86_64-darwin':

cmake_args += ['-D', 'CMAKE_OSX_ARCHITECTURES=x86_64']

elif fmi_platform == 'aarch64-darwin':

cmake_args += ['-D', 'CMAKE_OSX_ARCHITECTURES=arm64']

check_call(
['cmake'] +
cmake_args +
['-B', build_dir,
'-D', f'CMAKE_INSTALL_PREFIX={install_prefix}',
'-D', 'BUILD_SHARED_LIBS=OFF',
'-D', 'LIBXML2_WITH_ICONV=OFF',
'-D', 'LIBXML2_WITH_LZMA=OFF',
'-D', 'LIBXML2_WITH_PYTHON=OFF',
'-D', 'LIBXML2_WITH_ZLIB=OFF',
'-D', 'LIBXML2_WITH_TESTS=OFF',
root / 'libxml2-2.11.5']
)

check_call([
'cmake',
'--build', build_dir,
'--config', 'Release',
'--target', 'install'
])
73 changes: 73 additions & 0 deletions dist/build_zlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import tarfile
from pathlib import Path
from subprocess import check_call
from fmpy.util import download_file
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
'platform',
choices={'x86-windows', 'x86_64-windows', 'x86_64-linux', 'aarch64-linux', 'x86_64-darwin', 'aarch64-darwin'},
help="Platform to build for, e.g. x86_64-windows"
)
args, _ = parser.parse_known_args()

archive = download_file('https://www.zlib.net/fossils/zlib-1.3.tar.gz',
checksum='ff0ba4c292013dbc27530b3a81e1f9a813cd39de01ca5e0f8bf355702efa593e')

root = Path(__file__).parent

with tarfile.open(archive) as tf:
tf.extractall(root)

build_dir = root / f'zlib-{args.platform}' / 'build'

install_prefix = root / f'zlib-{args.platform}' / 'install'

cmake_args = []

fmi_platform = args.platform
fmi_architecture, fmi_system = fmi_platform.split('-')

if fmi_system == 'windows':

cmake_args += ['-G', 'Visual Studio 17 2022', '-A']

if fmi_architecture == 'x86':
cmake_args.append('Win32')
elif fmi_architecture == 'x86_64':
cmake_args.append('x64')

cmake_args += [
'-D', 'CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded'
#'-D', 'CMAKE_C_FLAGS_DEBUG=/MT /Zi /Ob0 /Od /RTC1',
#'-D', 'CMAKE_C_FLAGS_RELEASE=/MT /O2 /Ob2 /DNDEBUG'
]

elif fmi_platform == 'aarch64-linux':

toolchain_file = root / 'aarch64-linux-toolchain.cmake'
cmake_args += ['-D', f'CMAKE_TOOLCHAIN_FILE={ toolchain_file }']

elif fmi_platform == 'x86_64-darwin':

cmake_args += ['-D', 'CMAKE_OSX_ARCHITECTURES=x86_64']

elif fmi_platform == 'aarch64-darwin':

cmake_args += ['-D', 'CMAKE_OSX_ARCHITECTURES=arm64']

check_call(
['cmake'] +
cmake_args +
['-B', build_dir,
'-D', f'CMAKE_INSTALL_PREFIX={ install_prefix }',
root / 'zlib-1.3']
)

check_call([
'cmake',
'--build', build_dir,
'--config', 'Release',
'--target', 'install'
])
Loading

0 comments on commit adcff5f

Please sign in to comment.