Skip to content

Commit

Permalink
Merge pull request Spyro-Soft#402 from Spyro-Soft/develop
Browse files Browse the repository at this point in the history
Release 2.0.1
  • Loading branch information
fkuatspyro authored Nov 17, 2023
2 parents b14e1bc + d0121f3 commit 72f95dc
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 301 deletions.
2 changes: 1 addition & 1 deletion scargo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# #
# @copyright Copyright (C) 2023 SpyroSoft Solutions S.A. All rights reserved.
# #
__version__ = "2.0.0"
__version__ = "2.0.1"
25 changes: 11 additions & 14 deletions scargo/commands/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,17 @@ def scargo_publish(repo: str, profile: str = "Release") -> None:

# Export package
try:
subprocess.check_call(
subprocess.run(
[
"conan",
"export-pkg",
".",
"-of",
str(build_dir),
"-pr:b",
"default",
"-pr:h",
"-pr",
f"./config/conan/profiles/{config.project.target.family}_{profile}",
"-f",
"-of",
build_dir,
],
check=True,
cwd=project_path,
)
except subprocess.CalledProcessError:
Expand All @@ -60,35 +58,34 @@ def scargo_publish(repo: str, profile: str = "Release") -> None:

# Test if package has been exported successfully
try:
subprocess.check_call(
subprocess.run(
[
"conan",
"test",
"test_package",
f"{project_name}/{config.project.version}",
"-pr:b",
"default",
"-pr:h",
"-pr",
f"./config/conan/profiles/{config.project.target.family}_{profile}",
],
check=True,
cwd=project_path,
)
except subprocess.CalledProcessError:
logger.error("Package test failed")
sys.exit(1)

# Upload package to artifactory
# Upload package to conan remote
conan_repo = ["-r", repo] if repo else []
try:
subprocess.check_call(
subprocess.run(
[
"conan",
"upload",
f"{project_name}",
*conan_repo,
"--all",
"--confirm",
],
check=True,
cwd=project_path,
)
except subprocess.CalledProcessError:
Expand Down
4 changes: 2 additions & 2 deletions scargo/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from scargo.file_generators.readme_gen import generate_readme
from scargo.file_generators.tests_gen import generate_tests
from scargo.file_generators.vscode_gen import generate_vscode
from scargo.global_values import SCARGO_DOCKER_ENV, SCARGO_LOCK_FILE, SCARGO_PKG_PATH
from scargo.global_values import SCARGO_LOCK_FILE, SCARGO_PKG_PATH
from scargo.logger import get_logger

logger = get_logger()
Expand Down Expand Up @@ -96,7 +96,7 @@ def scargo_update(config_file_path: Path) -> None:
generate_readme(config)

# do not rebuild dockers in the docker
if project_config.build_env == SCARGO_DOCKER_ENV:
if project_config.is_docker_buildenv():
if not Path("/.dockerenv").exists():
if not pull_docker_image(docker_path):
scargo_docker_build([], config.project_root)
Expand Down
50 changes: 32 additions & 18 deletions scargo/conan_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import subprocess
from pathlib import Path
from typing import Dict, List

from scargo.config import Config
from scargo.logger import get_logger
Expand All @@ -16,8 +16,8 @@ def conan_add_remote(project_path: Path, config: Config) -> None:
:param Config config:
:return: None
"""
conan_repo = config.conan.repo
for repo_name, repo_url in conan_repo.items():
remotes_without_user = _get_remotes_without_user(config.conan.repo)
for repo_name, repo_url in config.conan.repo.items():
try:
subprocess.run(
["conan", "remote", "add", repo_name, repo_url],
Expand All @@ -29,30 +29,26 @@ def conan_add_remote(project_path: Path, config: Config) -> None:
if b"already exists in remotes" not in e.stderr:
logger.error(e.stderr.decode().strip())
logger.error("Unable to add remote repository")
conan_add_user(repo_name)
else:
pass
if repo_name in remotes_without_user:
conan_remote_login(repo_name)


def conan_add_user(remote: str) -> None:
def conan_remote_login(remote: str) -> None:
"""
Add conan user
:param str remote: name of remote repository
:return: None
"""
conan_user = subprocess.run(
"conan user", capture_output=True, shell=True, check=False
).stdout.decode("utf-8")
remote_login_command = ["conan", "remote", "login", remote]
logger.info("Login to conan remote %s", remote)

env_conan_user = os.environ.get("CONAN_LOGIN_USERNAME", "")
env_conan_passwd = os.environ.get("CONAN_PASSWORD", "")

if env_conan_user not in conan_user:
try:
subprocess.check_call(
["conan", "user", "-p", env_conan_passwd, "-r", remote, env_conan_user],
)
except subprocess.CalledProcessError:
logger.error("Unable to add user")
try:
subprocess.run(remote_login_command, check=True)
except subprocess.CalledProcessError:
logger.error(f"Unable to log in to conan remote {remote}")


def conan_source(project_dir: Path) -> None:
Expand All @@ -67,3 +63,21 @@ def conan_source(project_dir: Path) -> None:
)
except subprocess.CalledProcessError:
logger.error("Unable to source")


def _get_remotes_without_user(conan_remotes: Dict[str, str]) -> List[str]:
result = subprocess.run(
["conan", "remote", "list-users"], check=True, stdout=subprocess.PIPE
)
user_list_stdout = result.stdout.decode().splitlines()

no_users = []
for index, line in enumerate(user_list_stdout):
if remote_line := line.strip().split(":")[0]:
if (
remote_line in conan_remotes
and "No user" in user_list_stdout[index + 1]
):
no_users.append(remote_line)

return no_users
5 changes: 4 additions & 1 deletion scargo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import toml
from pydantic import BaseModel, Extra, Field, root_validator

from scargo.global_values import SCARGO_DEFAULT_BUILD_ENV
from scargo.global_values import SCARGO_DEFAULT_BUILD_ENV, SCARGO_DOCKER_ENV

CHIP_DEFAULTS = {
"x86": "",
Expand Down Expand Up @@ -134,6 +134,9 @@ def get_compiler_warning(self) -> Optional[str]:
return "Compiler settings are ignored for this target"
return None

def is_docker_buildenv(self) -> bool:
return self.build_env == SCARGO_DOCKER_ENV


class Target(BaseModel):
id: str
Expand Down
10 changes: 9 additions & 1 deletion scargo/config_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
from pathlib import Path
from typing import Optional
Expand All @@ -14,6 +15,12 @@
logger = get_logger()


def set_up_environment_variables(config: Config) -> None:
os.environ["SCARGO_PROJECT_ROOT"] = str(config.project_root.absolute())
if config.project.in_repo_conan_cache:
os.environ["CONAN_HOME"] = f"{config.project_root}/.conan2"


def get_scargo_config_or_exit(
config_file_path: Optional[Path] = None,
) -> Config:
Expand Down Expand Up @@ -41,12 +48,13 @@ def get_scargo_config_or_exit(

def prepare_config(run_in_docker: bool = True) -> Config:
"""
Prepare configuration file
Prepare configuration file and set up eniromnent variables
:return: project configuration
"""
config = get_scargo_config_or_exit()
check_scargo_version(config)
set_up_environment_variables(config)
if run_in_docker:
run_scargo_again_in_docker(config.project, config.project_root)
return config
Expand Down
4 changes: 1 addition & 3 deletions scargo/docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from docker import DockerClient

from scargo.config import ProjectConfig
from scargo.global_values import SCARGO_DOCKER_ENV
from scargo.logger import get_logger

logger = get_logger()
Expand Down Expand Up @@ -43,8 +42,7 @@ def run_scargo_again_in_docker(
:return: None
"""

build_env = project_config.build_env
if build_env != SCARGO_DOCKER_ENV or Path("/.dockerenv").exists():
if not project_config.is_docker_buildenv() or Path("/.dockerenv").exists():
return

cmd_args = sys.argv[1:]
Expand Down
4 changes: 2 additions & 2 deletions scargo/file_generators/base_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

logger = get_logger()

_TEMPLATE_ROOT = Path(SCARGO_PKG_PATH, "file_generators", "templates")
TEMPLATE_ROOT = Path(SCARGO_PKG_PATH, "file_generators", "templates")
_JINJA_ENV = Environment(
loader=FileSystemLoader(_TEMPLATE_ROOT),
loader=FileSystemLoader(TEMPLATE_ROOT),
trim_blocks=True,
lstrip_blocks=True,
)
Expand Down
15 changes: 12 additions & 3 deletions scargo/file_generators/conan_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# @copyright Copyright (C) 2023 SpyroSoft Solutions S.A. All rights reserved.
# #

import shutil
import subprocess
from pathlib import Path

from scargo.config import Config
from scargo.file_generators.base_gen import create_file_from_template
from scargo.file_generators.base_gen import TEMPLATE_ROOT, create_file_from_template


def generate_conanfile(config: Config) -> None:
Expand All @@ -28,11 +30,18 @@ def generate_conanprofile(config: Config) -> None:

if config.project.target.family == "stm32":
create_file_from_template(
"conan/stm32_gcc_toolchain_wrapper.cmake.j2",
"config/conan/profiles/stm32_gcc_toolchain_wrapper.cmake",
"conan/toolchain/stm32_gcc_toolchain.cmake.j2",
"config/conan/profiles/stm32_gcc_toolchain.cmake",
template_params={"config": config},
config=config,
)
elif config.project.target.family == "atsam":
outpath = Path("config/conan/profiles/arm_gcc_toolchain.cmake")
outpath.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(
TEMPLATE_ROOT / "conan/toolchain/arm_gcc_toolchain.cmake",
outpath,
)

for profile in profiles:
create_file_from_template(
Expand Down
2 changes: 1 addition & 1 deletion scargo/file_generators/templates/conan/profile_atsam.j2
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CXX=arm-none-eabi-g++
[conf]
tools.build:cflags=["{{ config.project.cflags if config.project.cflags}} {{config.profiles.get(profile).cflags if config.profiles.get(profile).cflags}}"]
tools.build:cxxflags=["{{ config.project.cxxflags if config.project.cxxflags }} {{ config.profiles.get(profile).cxxflags if config.profiles.get(profile).cxxflags }}"]

{% if config.project.max_build_jobs != None %}
tools.build:jobs={{config.project.max_build_jobs}}
{% endif %}
tools.cmake.cmaketoolchain:user_toolchain=["{{ '{{ os.path.join(profile_dir, "arm_gcc_toolchain.cmake") }}' }}"]
3 changes: 1 addition & 2 deletions scargo/file_generators/templates/conan/profile_stm32.j2
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ tools.build:cxxflags=["{{ config.project.cxxflags if config.project.cxxflags }}
{% if config.project.max_build_jobs != None %}
tools.build:jobs={{config.project.max_build_jobs}}
{% endif %}
{% set workspace_dir = config.project_root if config.project.build_env != "docker" else "/workspace" %}
tools.cmake.cmaketoolchain:user_toolchain=["{{ workspace_dir }}/config/conan/profiles/stm32_gcc_toolchain_wrapper.cmake"]
tools.cmake.cmaketoolchain:user_toolchain=["{{ '{{ os.path.join(profile_dir, "stm32_gcc_toolchain.cmake") }}' }}"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)

set(TOOLCHAIN_PREFIX arm-none-eabi-)
find_program(BINUTILS_PATH ${TOOLCHAIN_PREFIX}gcc NO_CACHE)

if (NOT BINUTILS_PATH)
message(FATAL_ERROR "ARM GCC toolchain not found")
endif ()

get_filename_component(ARM_TOOLCHAIN_DIR ${BINUTILS_PATH} DIRECTORY)
# Without that flag CMake is not able to pass test compilation check
if (${CMAKE_VERSION} VERSION_EQUAL "3.6.0" OR ${CMAKE_VERSION} VERSION_GREATER "3.6")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
else ()
set(CMAKE_EXE_LINKER_FLAGS_INIT "--specs=nosys.specs")
endif ()

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_AR ${TOOLCHAIN_PREFIX}gcc-ar)
set(CMAKE_RANLIB ${TOOLCHAIN_PREFIX}gcc-ranlib)

execute_process(COMMAND ${CMAKE_C_COMPILER} -print-sysroot
OUTPUT_VARIABLE ARM_GCC_SYSROOT OUTPUT_STRIP_TRAILING_WHITESPACE)

# Default C compiler flags
set(CMAKE_C_FLAGS_DEBUG_INIT "-g3 -Og -Wall -DDEBUG")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG_INIT}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELEASE_INIT "-O3 -Wall")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE_INIT}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "-Os -Wall")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL_INIT}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g -Wall")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING "" FORCE)
# Default C++ compiler flags
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g3 -Og -Wall -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG_INIT}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE_INIT}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_MINSIZEREL_INIT "-Os -Wall")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL_INIT}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g -Wall")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING "" FORCE)

set(CMAKE_OBJCOPY ${ARM_TOOLCHAIN_DIR}/${TOOLCHAIN_PREFIX}objcopy CACHE INTERNAL "objcopy tool")
set(CMAKE_SIZE_UTIL ${ARM_TOOLCHAIN_DIR}/${TOOLCHAIN_PREFIX}size CACHE INTERNAL "size tool")

set(CMAKE_SYSROOT ${ARM_GCC_SYSROOT})
set(CMAKE_FIND_ROOT_PATH ${BINUTILS_PATH})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% set workspace_dir = config.project_root if config.project.build_env != "docker" else "/workspace" %}
include({{ workspace_dir }}/third-party/stm32-cmake/cmake/stm32_gcc.cmake)
include($ENV{SCARGO_PROJECT_ROOT}/third-party/stm32-cmake/cmake/stm32_gcc.cmake)

stm32_get_chip_info($ENV{STM32_CHIP} FAMILY STM32_FAMILY DEVICE STM32_DEVICE TYPE STM32_TYPE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ services:
- ..:/workspace
- /dev:/dev
command: sleep infinity
{% if config.project.in_repo_conan_cache %}
environment:
- CONAN_USER_HOME=/workspace
{% endif %}
{% if config.docker_compose.ports %}
ports:
{% for port_ranges in config.docker_compose.ports %}
Expand Down
1 change: 0 additions & 1 deletion scargo/file_generators/templates/stm32.cmake.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% include 'project.cmake.j2' %}

stm32_get_chip_info($ENV{STM32_CHIP} FAMILY STM32_FAMILY DEVICE STM32_DEVICE TYPE STM32_TYPE)

message("Fetching stm32 cube for stm32 ${STM32_FAMILY} family")
stm32_fetch_cube(${STM32_FAMILY})
2 changes: 1 addition & 1 deletion scargo/templates/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ __pycache__/
package-lock.json
.env
.devcontainer/.env
.conan
.conan2

### Linux ###
*~
Expand Down
Loading

0 comments on commit 72f95dc

Please sign in to comment.