diff --git a/dev/README.md b/dev/README.md index c52a42d97..d8779b47d 100644 --- a/dev/README.md +++ b/dev/README.md @@ -1,8 +1,15 @@ +[cmake]: https://cmake.org/ [cmake ext]: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools [cmake presets]: https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html [cpp ext]: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools [clangd ext]: https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd +[dotnet]: https://dotnet.microsoft.com/en-us/download/dotnet +[ninja build]: https://ninja-build.org/ +[ninja multi-config generator]: https://cmake.org/cmake/help/latest/generator/Ninja%20Multi-Config.html +[python]: https://www.python.org/ [py venv]: https://docs.python.org/3/library/venv.html +[visual studio]: https://visualstudio.microsoft.com/ +[visual studio generator]: https://cmake.org/cmake/help/latest/generator/Visual%20Studio%2017%202022.html # C++ Intellisense for Spark on Windows (VS Code) **Steps** @@ -74,3 +81,95 @@ Preset**, then select the **[preset][cmake presets]** you wish to build from the dropdown 3. Run **CMake: Build** in the command palette. + +# Build Spark Api Server on Windows +### **Dependencies** +* **[CMake][cmake]** (version >= 3.23) +* **[Python][python]** (version >= 3.9) +* **[Visual Studio][visual studio]** (version) +* **[Dotnet][dotnet]** (sdk version >= 7.0.0) + +### **References** +* **[CMakePresets][cmake presets]** +* **[Visual Studio 17 2022 (CMake generator)][visual studio generator]** +* **[Python Virtual Environment][py venv]** + +### Notes +* *Run commands from the repo root dir unless otherwise noted* + +1. Set up the **[Python Virtual Environment][py venv]** + ``` + # Create the virtual environment + python3 -m venv .venv + + # Activate the virtual environment + .venv\bin\activate.bat + + # Update pip + python -m pip install --upgrade pip + + # Install packages + pip install -r requirements.txt + ``` +2. Build + * *Run in python virtual environment* + ``` + # Configure + cmake --preset spark-api-server-windows + + # Build debug + cmake --build --preset spark-api-server-windows-debug + dotnet build Spark.Api.Server.sln --configuration Debug + + # Build release + cmake --build --preset spark-api-server-windows-release + dotnet build Spark.Api.Server.sln --configuration Release + ``` + +# Build Spark Api Server on Linux +### **Dependencies** +* **[CMake][cmake]** (version >= 3.23) +* **[Ninja Build][ninja build]** +* **[Python][python]** (version >= 3.9) +* **[Dotnet][dotnet]** (sdk version >= 7.0.0) + +### **References** +* **[CMakePresets][cmake presets]** +* **[Ninja Multi-Config (CMake generator)][ninja multi-config generator]** +* **[Python Virtual Environment][py venv]** + +### Prerequisites +* Open repository in a VS Code [Devcontainer](../docker/sparkd/dev/README.md), or + create your own [docker container](../docker/sparkd/dev/README.md) in which to build + +### Notes +* *Run commands from the repo root dir unless otherwise noted* + +1. Set up the **[Python Virtual Environment][py venv]** + ``` + # Create the virtual environment + python3 -m venv .venv + + # Activate the virtual environment + . .venv/bin/activate + + # Update pip + python -m pip install --upgrade pip + + # Install packages + pip install -r requirements.txt + ``` +2. Build + * *Run in python virtual environment* + ``` + # Configure + cmake --preset spark-api-server-linux + + # Build debug + cmake --build --preset spark-api-server-linux-debug + dotnet build Spark.Api.Server.sln --configuration Debug + + # Build release + cmake --build --preset spark-api-server-linux-release + dotnet build Spark.Api.Server.sln --configuration Release + ``` diff --git a/tools/py/build_api_server.py b/tools/py/build_api_server.py deleted file mode 100644 index 5ad448b85..000000000 --- a/tools/py/build_api_server.py +++ /dev/null @@ -1,245 +0,0 @@ -import argparse -import subprocess -import os - -from typing import Optional -from cmake.cmake_utils import CMakeCompilerPair, CMakeConfigureCommand -from core.argparse_utils import add_toggle_opt, get_arg -from core.file_utils import CWD, SparkPaths, as_path, create_dir, link_file -from core.iter_utils import flatten, is_none_or_empty -from core.logging import exit_msg - - -def _parse_compiler(arg: str or None): - '''Parses compiler input (from cli) and returns a tuple of c, cxx compilers respectively''' - known_compilers = { - 'gcc': CMakeCompilerPair('gcc', 'gcc-11', 'g++-11'), - 'clang': CMakeCompilerPair('clang', 'clang', 'clang++'), - 'clang-13': CMakeCompilerPair('clang', 'clang-13', 'clang++-13'), - } - if arg in known_compilers: - return known_compilers[arg] - raise ValueError( - f"Unexpected compiler '{arg}'. Please use one of [{' '.join(known_compilers)}]" - ) - - -def _parse_build_dir(build_dir, build_type: Optional[str] = None): - if build_dir is not None: - return as_path(build_dir).value() - rv = SparkPaths.root().joinpath('build') - if build_type is not None: - rv = rv.joinpath(build_type) - return rv - - -class BuildApiServerArgs: - def __init__(self, args): - args_dict = vars(args) - self.compiler = _parse_compiler(get_arg(args_dict, 'compiler', 'gcc')) - # Forcibly correct case (so user doesn't have to worry abt it) - self.build_type = args.build_type.title() - if self.build_type not in ['Debug', 'Release']: - raise ValueError(f"Unexpected build_type '{args.build_type}'") - self.cmake_build_dir = _parse_build_dir( - args_dict.get('cmake_output_dir')).absolute() - self.clean = args.clean - self.jobs = args.jobs - self.targets = None if args.target is None else flatten(args.target) - self.dotnet_verbosity = args.verbosity - # Translate dotnet verbosity level to binary '--verbose' flag for cmake - self.cmake_verbose = args.verbosity in [ - 'd', 'detailed', 'diag', 'diagnostic' - ] - - -def _get_arg_parser(): - parser = argparse.ArgumentParser(description='Build spark_client.sln') - - parser.add_argument( - '--build-type', - metavar='debug|release', - type=str, - nargs='?', - help='The build type') - - parser.add_argument( - '--compiler', - metavar='gcc|clang', - type=str, - nargs='?', - help='The compiler') - - parser.add_argument( - '-o', - '--cmake-output-dir', - metavar='path', - type=str, - nargs='?', - help= - 'The directory in which to build libspark (defaults to [repo_root]/build)' - ) - - parser.add_argument( - '-t', - '--target', - type=str, - action='append', - nargs='+', - help='The target(s) to build') - - parser.add_argument( - '-j', - '--jobs', - type=int, - help='[CMake] The number of parallel jobs to run') - - add_toggle_opt( - parser, 'clean', help='Whether or not to perform a clean build') - - parser.add_argument( - '--verbosity', - metavar='[q(uiet)|m(inimal)|n(ormal)|d(etailed)|diag(nostic)]', - choices=[ - 'q', 'quiet', 'm', 'minimal', 'n', 'normal', 'd', 'detailed', - 'diag', 'diagnostic' - ], - type=str, - help='The output verbosity level') - - parser.set_defaults( - build_type='Debug', compiler='gcc', verbosity='minimal') - return parser - - -def _configure(src_dir, build_dir, compiler: CMakeCompilerPair): - '''Configures sparkd (server) build''' - - cmd = CMakeConfigureCommand( - src_dir=src_dir, - build_dir=build_dir, - generator='"Ninja Multi-Config"', - compiler=compiler) - cmd.add_opt('BUILD_API_SERVER', True) - cmd.add_opt('BUILD_SPARKD', False) - cmd.add_opt('BUILD_WEB', False) - cmd.add_opt('CMAKE_EXPORT_COMPILE_COMMANDS', True) - return _run_cmd(str(cmd)) - - -def _check_results(proc_results): - for res in proc_results: - if res.returncode != 0: - exit_msg(1, f'The following build subprocess failed: {res}') - - -def _run_cmd(cmd): - print(f'run cmd: {cmd}') - return subprocess.run(cmd, shell=True) - - -def _dotnet_build(build_type='Debug', verbosity='minimal'): - '''Builds dotnet''' - with (CWD(SparkPaths.root())): - cmd_parts = [f'dotnet build Spark.Api.Server.sln'] - if verbosity is not None: - cmd_parts.append(f'--verbosity {verbosity}') - if build_type is not None: - cmd_parts.append(f'--configuration {build_type}') - return _run_cmd(' '.join(cmd_parts)) - - -def _dotnet_clean(build_type='Debug', verbosity='minimal'): - '''Cleans dotnet''' - with (CWD(SparkPaths.root())): - cmd_parts = [f'dotnet clean Spark.Api.Server.sln'] - if verbosity is not None: - cmd_parts.append(f'--verbosity {verbosity}') - if build_type is not None: - cmd_parts.append(f'--configuration {build_type}') - return _run_cmd(' '.join(cmd_parts)) - - -def _cmake_build( - build_dir, build_type=None, targets=None, jobs=None, verbose=False): - '''Builds libspark''' - - build_type = build_type or 'release' - build_dir = as_path(build_dir).value() - cmd_parts = [f'cmake --build {build_dir} --config {build_type.title()}'] - if jobs is not None: - cmd_parts.append(f'-j {jobs}') - if not is_none_or_empty(targets): - cmd_parts.append(' '.join([f"--target {t}" for t in targets])) - if verbose: - cmd_parts.append('--verbose') - return _run_cmd(' '.join(cmd_parts)) - - -def _cmake_clean(build_dir, build_type=None, jobs=None, verbose=False): - '''Cleans libspark''' - # In cmake, 'clean' is technically a build target - _cmake_build(build_dir, build_type, ['clean'], jobs=jobs, verbose=verbose) - - -def _build( - src_dir, - cmake_build_dir, - build_type: str, - compiler=None, - targets=None, - clean=False, - jobs=None, - cmake_verbose=False, - dotnet_verbosity: Optional[str] = None) -> list: - '''Builds libspark''' - - # check if explicit no-op - if targets is not None and len(targets) == 0: - return [] - - compiler = compiler or CMakeCompilerPair('gcc', 'gcc', 'g++') - cmake_build_dir = as_path(cmake_build_dir).value() - proc_results = [] - - try: - create_dir(cmake_build_dir) - proc_results.append( - _configure( - src_dir=src_dir, build_dir=cmake_build_dir, compiler=compiler)) - if proc_results[-1].returncode: - # Build is broken (exit early) - return proc_results - except Exception as e: - exit_msg(1, e) - - if clean: - # cmake clean - proc_results.append( - _cmake_clean(cmake_build_dir, build_type, jobs, cmake_verbose)) - # dotnet clean - proc_results.append(_dotnet_clean(build_type, dotnet_verbosity)) - - # cmake build - proc_results.append( - _cmake_build( - cmake_build_dir, build_type, targets, jobs, cmake_verbose)) - return proc_results - - -def _main(): - args = BuildApiServerArgs(_get_arg_parser().parse_args()) - proc_results = _build( - src_dir=SparkPaths.root(), - cmake_build_dir=args.cmake_build_dir, - build_type=args.build_type, - compiler=args.compiler, - targets=args.targets, - jobs=args.jobs, - cmake_verbose=args.cmake_verbose, - dotnet_verbosity=args.dotnet_verbosity) - _check_results(proc_results) - - -if __name__ == '__main__': - _main()