Skip to content

Commit

Permalink
Build executables.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Oct 11, 2023
1 parent 6f34846 commit de4275f
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 13 deletions.
106 changes: 106 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright (C) 2023 Toitware ApS. All rights reserved.

name: CI

on:
push:
branches:
- "*"
- "*/*"

env:
TOIT_VERSION: v2.0.0-alpha.114

jobs:
build:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3

- name: Setup Toit
shell: bash
run: |
if [[ "$RUNNER_OS" = "Windows" ]]; then
BIN_EXTENSION=".exe"
fi
echo "BIN_EXTENSION=$BIN_EXTENSION" >> $GITHUB_ENV
export NATIVE_DOWNLOAD_DIR="${{ github.workspace }}/downloads"
echo "NATIVE_DOWNLOAD_DIR=$NATIVE_DOWNLOAD_DIR" >> $GITHUB_ENV
export DOWNLOAD_DIR="$PWD/downloads"
echo "DOWNLOAD_DIR=$DOWNLOAD_DIR" >> $GITHUB_ENV
TOIT_SDK_DIR=$DOWNLOAD_DIR/toit
echo "TOIT_EXEC=$TOIT_SDK_DIR/bin/toit.run$BIN_EXTENSION" >> $GITHUB_ENV
echo "TOITC_EXEC=$TOIT_SDK_DIR/bin/toit.compile$BIN_EXTENSION" >> $GITHUB_ENV
echo "TPKG_EXEC=$TOIT_SDK_DIR/bin/toit.pkg$BIN_EXTENSION" >> $GITHUB_ENV
TOIT_SDK_FILE=toit-$(echo $RUNNER_OS | tr '[:upper:]' '[:lower:]').tar.gz
TOIT_SDK_BASE_URL=https:/toitlang/toit/releases
echo "TOIT_SDK_URL=$TOIT_SDK_BASE_URL/download/$TOIT_VERSION/$TOIT_SDK_FILE" >> $GITHUB_ENV
- uses: suisei-cn/[email protected]
name: Download Toit
with:
url: ${{ env.TOIT_SDK_URL }}
target: ${{ env.NATIVE_DOWNLOAD_DIR }}

- name: Extract Toit
shell: bash
run: |
cd "$DOWNLOAD_DIR"
for f in *.tar.gz; do
tar x -f $f
done
ls $TOIT_EXEC
ls $TOITC_EXEC
ls $TPKG_EXEC
# Fetch the dependencies. Different for each platform.
- name: Install dependencies - Linux
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install ninja-build
ninja --version
cmake --version
- name: Install dependencies - macOS
if: runner.os == 'macOS'
run: |
brew install ninja
ninja --version
cmake --version
- name: Install dependencies - Windows
if: runner.os == 'Windows'
run: |
choco install ninja
ninja --version
cmake --version
- name: Run cmake
shell: bash
run: |
make rebuild-cmake
cmake \
-DTOITC="$TOITC_EXEC" \
-DTOITPKG="$TPKG_EXEC" \
-DTOITRUN="$TOIT_EXEC" \
build
- name: Build binaries
shell: bash
run: |
make
- name: Upload binary artifacts
uses: actions/upload-artifact@v3
with:
name: binaries-${{ runner.os }}
path: build/partitions
18 changes: 14 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ cmake_minimum_required(VERSION 3.23)

project(partition-table)

# Add windows exe extension.
set(TOIT_EXEC "toit.run${CMAKE_EXECUTABLE_SUFFIX}" CACHE FILEPATH "The executable used to run the tests")
set(TPKG_EXEC "toit.pkg${CMAKE_EXECUTABLE_SUFFIX}" CACHE FILEPATH "The executable used to install the packages")
set(TCOMPILE_EXEC "toit.compile${CMAKE_EXECUTABLE_SUFFIX}" CACHE FILEPATH "The executable used to check the syntax")
set(TOITPKG
"toit.pkg${CMAKE_EXECUTABLE_SUFFIX}"
CACHE
FILEPATH
"The executable used to install packages")
set(TOITC
"toit.compile${CMAKE_EXECUTABLE_SUFFIX}"
CACHE
FILEPATH
"The executable used to compile programs and check the syntax")

set(DEFAULT_SDK_VERSION CACHE STRING "The default SDK version to use")

Expand All @@ -27,5 +33,9 @@ configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/bin/version.toit
@ONLY)

include("tools/toit.cmake")

add_custom_target(build)

enable_testing()
add_subdirectory(bin)
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
# Use of this source code is governed by a Zero-Clause BSD license that can
# be found in the tests/TESTS_LICENSE file.

all: build/CMakeCache.txt
.PHONY: all
all: build

.PHONY: build
build: rebuild-cmake install-pkgs
(cd build && ninja build)

.PHONY: build/CMakeCache.txt
build/CMakeCache.txt:
$(MAKE) rebuild-cmake

.PHONY: install-pkgs
install-pkgs: rebuild-cmake
(cd build && ninja install-pkgs)
(cd build && ninja download_packages)

# We rebuild the cmake file all the time.
# We use "glob" in the cmakefile, and wouldn't otherwise notice if a new
# file (for example a test) was added or removed.
# It takes <1s on Linux to run cmake, so it doesn't hurt to run it frequently.
.PHONY: rebuild-cmake
rebuild-cmake:
mkdir -p build
(cd build && cmake .. -G Ninja)

.PHONY: all test rebuild-cmake install-pkgs

21 changes: 17 additions & 4 deletions bin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
# Use of this source code is governed by an MIT-style license that can be
# found in the LICENSE file.

message("TPKG: ${TPKG_EXEC}")
toit_project(partition "${CMAKE_CURRENT_LIST_DIR}")

set(PARTITIONS_SOURCE "${CMAKE_CURRENT_LIST_DIR}/partitions.toit")
set(PARTITIONS_EXE "${CMAKE_BINARY_DIR}/partitions${CMAKE_EXECUTABLE_SUFFIX}")
set(PARTITIONS_DEP "${CMAKE_CURRENT_BINARY_DIR}/partitions.dep")

ADD_TOIT_EXE(
${PARTITIONS_SOURCE}
${PARTITIONS_EXE}
${PARTITIONS_DEP}
""
)

add_custom_target(
"install-pkgs"
COMMAND "${TPKG_EXEC}" install
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
build_partitions
DEPENDS ${PARTITIONS_EXE}
)

add_dependencies(build build_partitions)
118 changes: 118 additions & 0 deletions tools/toit.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright (C) 2019 Toitware ApS.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; version
# 2.1 only.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# The license can be found in the file `LICENSE` in the top level
# directory of this repository.

# This file serves as normal cmake include, as well as a cmake-script, run with
# `cmake -P`.
# In the latter case the `EXECUTING_SCRIPT` variable is defined, and we only
# process the command that we should execute.
set(TOIT_DOWNLOAD_PACKAGE_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/toit.cmake")
if (DEFINED EXECUTING_SCRIPT)
if ("${SCRIPT_COMMAND}" STREQUAL "install_packages")
if (NOT DEFINED TOIT_PROJECT)
message(FATAL_ERROR "Missing TOIT_PROJECT")
endif()
if (NOT DEFINED TOITPKG)
message(FATAL_ERROR "Missing TOITPKG")
endif()

if (EXISTS "${TOIT_PROJECT}/package.yaml" OR EXISTS "${TOIT_PROJECT}/package.lock")
execute_process(
COMMAND "${TOITPKG}" install --auto-sync=false "--project-root=${TOIT_PROJECT}"
COMMAND_ERROR_IS_FATAL ANY
)
endif()
else()
message(FATAL_ERROR "Unknown script command ${SCRIPT_COMMAND}")
endif()

# End the execution of this file.
return()
endif()

# Creates a custom command to build ${TARGET} with correct dependencies.
function(ADD_TOIT_SNAPSHOT SOURCE TARGET DEP_FILE ENV)
if (NOT DEFINED TOITC)
set(TOITC "$ENV{TOITC}")
if ("${TOITC}" STREQUAL "")
# TOITC is normally set to the toit.compile executable.
# However, for cross-compilation the compiler must be provided manually.
message(FATAL_ERROR "TOITC not provided")
endif()
endif()
if(POLICY CMP0116)
cmake_policy(SET CMP0116 NEW)
endif()
add_custom_command(
OUTPUT "${TARGET}"
DEPFILE ${DEP_FILE}
DEPENDS download_packages "${SOURCE}"
COMMAND ${CMAKE_COMMAND} -E env ${ENV} ASAN_OPTIONS=detect_leaks=false "${TOITC}" --dependency-file "${DEP_FILE}" --dependency-format ninja -w "${TARGET}" "${SOURCE}"
)
endfunction(ADD_TOIT_SNAPSHOT)

# Creates a custom command to build ${TARGET} with correct dependencies.
function(ADD_TOIT_EXE SOURCE TARGET DEP_FILE ENV)
if (NOT DEFINED TOITC)
set(TOITC "$ENV{TOITC}")
if ("${TOITC}" STREQUAL "")
# TOITC is normally set to the toit.compile executable.
# However, for cross-compilation the compiler must be provided manually.
message(FATAL_ERROR "TOITC not provided")
endif()
endif()
if(POLICY CMP0116)
cmake_policy(SET CMP0116 NEW)
endif()
add_custom_command(
OUTPUT "${TARGET}"
DEPFILE ${DEP_FILE}
DEPENDS download_packages "${SOURCE}"
COMMAND ${CMAKE_COMMAND} -E env ${ENV} ASAN_OPTIONS=detect_leaks=false "${TOITC}" --dependency-file "${DEP_FILE}" --dependency-format ninja -o "${TARGET}" "${SOURCE}"
)
endfunction(ADD_TOIT_EXE)

macro(toit_project NAME PATH)
if (NOT DEFINED TOITPKG)
set(TOITPKG "$ENV{TOITPKG}")
if ("${TOITPKG}" STREQUAL "")
# TOITPKG is normally set to the toit.pkg executable.
# However, for cross-compilation the compiler must be provided manually.
message(FATAL_ERROR "TOITPKG not provided")
endif()
endif()

if (NOT TARGET download_packages)
add_custom_target(
download_packages
)
add_custom_target(
sync_packages
COMMAND "${TOITPKG}" sync
)
endif()

set(DOWNLOAD_TARGET_NAME "download-${NAME}-packages")
add_custom_target(
"${DOWNLOAD_TARGET_NAME}"
COMMAND "${CMAKE_COMMAND}"
-DEXECUTING_SCRIPT=true
-DSCRIPT_COMMAND=install_packages
"-DTOIT_PROJECT=${PATH}"
"-DTOITPKG=${TOITPKG}"
-P "${TOIT_DOWNLOAD_PACKAGE_SCRIPT}"
)
add_dependencies(download_packages "${DOWNLOAD_TARGET_NAME}")
add_dependencies("${DOWNLOAD_TARGET_NAME}" sync_packages)
endmacro()

0 comments on commit de4275f

Please sign in to comment.