Skip to content

Commit

Permalink
Some general refactoring in the builder / project code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Sep 23, 2023
1 parent ad776c7 commit 709fe1c
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 306 deletions.
104 changes: 19 additions & 85 deletions src/build/build_options.h → src/build/build.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

// Copyright (c) 2019 Christoffer Lerno. All rights reserved.
// Copyright (c) 2019-2023 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by the GNU LGPLv3.0 license
// a copy of which can be found in the LICENSE file.

Expand All @@ -14,8 +13,6 @@
#define DEFAULT_SYMTAB_SIZE (256 * 1024)
#define DEFAULT_SWITCHRANGE_MAX_SIZE (256)

void update_feature_flags(const char ***flags, const char ***removed_flag, const char *arg, bool add);

typedef enum
{
BACKEND_LLVM = 1,
Expand Down Expand Up @@ -228,57 +225,6 @@ typedef enum
RISCVFLOAT_DOUBLE = 2,
} RiscvFloatCapability;


static const char *x86_vector_capability[6] = {
[X86VECTOR_NONE] = "none",
[X86VECTOR_MMX] = "mmx",
[X86VECTOR_SSE] = "sse",
[X86VECTOR_AVX] = "avx",
[X86VECTOR_AVX512] = "avx512",
[X86VECTOR_NATIVE] = "native"
};

static const char *x86_cpu_set[8] = {
[X86CPU_BASELINE] = "baseline",
[X86CPU_SSSE3] = "ssse3",
[X86CPU_SSE4] = "sse4",
[X86CPU_AVX1] = "avx1",
[X86CPU_AVX2_V1] = "avx2-v1",
[X86CPU_AVX2_V2] = "avx2-v2",
[X86CPU_AVX512] = "avx512",
[X86CPU_NATIVE] = "native"
};

static const char *fp_math[3] = {
[FP_STRICT] = "strict",
[FP_RELAXED] = "relaxed",
[FP_FAST] = "fast",
};

static const char *optlevels[4] = {
[OPTIMIZATION_NONE] = "none",
[OPTIMIZATION_LESS] = "less",
[OPTIMIZATION_MORE] = "more",
[OPTIMIZATION_AGGRESSIVE] = "max",
};

static const char *optsizes[3] = {
[SIZE_OPTIMIZATION_NONE] = "none",
[SIZE_OPTIMIZATION_SMALL] = "small",
[SIZE_OPTIMIZATION_TINY] = "more",
};

static const char *on_off[2] = {
[SAFETY_OFF] = "no",
[SAFETY_ON] = "yes",
};

static const char *riscv_capability[3] = {
[RISCVFLOAT_NONE] = "none",
[RISCVFLOAT_FLOAT] = "float",
[RISCVFLOAT_DOUBLE] = "double",
};

typedef enum
{
MEMORY_ENV_NOT_SET = -1,
Expand All @@ -288,13 +234,6 @@ typedef enum
MEMORY_ENV_NONE = 3,
} MemoryEnvironment;

static const char *memory_environment[6] = {
[MEMORY_ENV_NORMAL] = "normal",
[MEMORY_ENV_SMALL] = "small",
[MEMORY_ENV_TINY] = "tiny",
[MEMORY_ENV_NONE] = "none",
};

typedef enum
{
WIN_CRT_DEFAULT = -1,
Expand All @@ -303,12 +242,6 @@ typedef enum
WIN_CRT_STATIC = 2,
} WinCrtLinking;

static const char *wincrt_linking[3] = {
[WIN_CRT_NONE] = "none",
[WIN_CRT_DYNAMIC] = "dynamic",
[WIN_CRT_STATIC] = "static",
};

typedef enum
{
RELOC_DEFAULT = -1,
Expand All @@ -319,14 +252,6 @@ typedef enum
RELOC_BIG_PIE = 4,
} RelocModel;

static const char *reloc_models[5] = {
[RELOC_NONE] = "none",
[RELOC_SMALL_PIC] = "pic",
[RELOC_BIG_PIC] = "PIC",
[RELOC_SMALL_PIE] = "pie",
[RELOC_BIG_PIE] = "PIE",
};

typedef enum
{
DEBUG_INFO_NOT_SET = -1,
Expand Down Expand Up @@ -454,9 +379,6 @@ typedef struct BuildOptions_
bool testing;
} BuildOptions;




typedef enum
{
TARGET_TYPE_EXECUTABLE,
Expand Down Expand Up @@ -580,11 +502,16 @@ typedef struct
} linuxpaths;
} BuildTarget;


BuildOptions parse_arguments(int argc, const char *argv[]);
ArchOsTarget arch_os_target_from_string(const char *target);
bool command_is_projectless(CompilerCommand command);
void update_build_target_with_opt_level(BuildTarget *target, OptimizationSetting level);
static const char *x86_cpu_set[8] = {
[X86CPU_BASELINE] = "baseline",
[X86CPU_SSSE3] = "ssse3",
[X86CPU_SSE4] = "sse4",
[X86CPU_AVX1] = "avx1",
[X86CPU_AVX2_V1] = "avx2-v1",
[X86CPU_AVX2_V2] = "avx2-v2",
[X86CPU_AVX512] = "avx512",
[X86CPU_NATIVE] = "native"
};

static BuildTarget default_build_target = {
.optlevel = OPTIMIZATION_NOT_SET,
Expand Down Expand Up @@ -616,4 +543,11 @@ static BuildTarget default_build_target = {
.feature.safe_mode = SAFETY_NOT_SET,
.win.crt_linking = WIN_CRT_DEFAULT,
.switchrange_max_size = DEFAULT_SWITCHRANGE_MAX_SIZE,
};
};

BuildOptions parse_arguments(int argc, const char *argv[]);
ArchOsTarget arch_os_target_from_string(const char *target);
bool command_accepts_files(CompilerCommand command);
void update_build_target_with_opt_level(BuildTarget *target, OptimizationSetting level);
void create_project(BuildOptions *build_options);

68 changes: 63 additions & 5 deletions src/build/build_internal.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
#pragma once

// Copyright (c) 2020 Christoffer Lerno. All rights reserved.
// Copyright (c) 2020-2023 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by a LGPLv3.0
// a copy of which can be found in the LICENSE file.

#include "utils/lib.h"
#include "utils/json.h"
#include "build_options.h"

#include "build.h"

typedef struct
{
BuildTarget **targets;
} Project;

static const char *memory_environment[6] = {
[MEMORY_ENV_NORMAL] = "normal",
[MEMORY_ENV_SMALL] = "small",
[MEMORY_ENV_TINY] = "tiny",
[MEMORY_ENV_NONE] = "none",
};

static const char *wincrt_linking[3] = {
[WIN_CRT_NONE] = "none",
[WIN_CRT_DYNAMIC] = "dynamic",
[WIN_CRT_STATIC] = "static",
};

static const char *optsizes[3] = {
[SIZE_OPTIMIZATION_NONE] = "none",
[SIZE_OPTIMIZATION_SMALL] = "small",
[SIZE_OPTIMIZATION_TINY] = "tiny",
};

static const char *on_off[2] = {
[SAFETY_OFF] = "no",
[SAFETY_ON] = "yes",
};

static const char *riscv_capability[3] = {
[RISCVFLOAT_NONE] = "none",
[RISCVFLOAT_FLOAT] = "float",
[RISCVFLOAT_DOUBLE] = "double",
};

static const char *fp_math[3] = {
[FP_STRICT] = "strict",
[FP_RELAXED] = "relaxed",
[FP_FAST] = "fast",
};

static const char *x86_vector_capability[6] = {
[X86VECTOR_NONE] = "none",
[X86VECTOR_MMX] = "mmx",
[X86VECTOR_SSE] = "sse",
[X86VECTOR_AVX] = "avx",
[X86VECTOR_AVX512] = "avx512",
[X86VECTOR_NATIVE] = "native"
};

static const char *optlevels[4] = {
[OPTIMIZATION_NONE] = "none",
[OPTIMIZATION_LESS] = "less",
[OPTIMIZATION_MORE] = "more",
[OPTIMIZATION_AGGRESSIVE] = "max",
};

static const char *reloc_models[5] = {
[RELOC_NONE] = "none",
[RELOC_SMALL_PIC] = "pic",
[RELOC_BIG_PIC] = "PIC",
[RELOC_SMALL_PIE] = "pie",
[RELOC_BIG_PIE] = "PIE",
};

Project *project_load(void);
BuildTarget *project_select_target(Project *project, const char *optional_target);

void update_feature_flags(const char ***flags, const char ***removed_flag, const char *arg, bool add);
102 changes: 52 additions & 50 deletions src/build/build_options.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
// Copyright (c) 2019 Christoffer Lerno. All rights reserved.
// Copyright (c) 2019-2023 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by the GNU LGPLv3.0 license
// a copy of which can be found in the LICENSE file.

#include "build_options.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <stdbool.h>
#include <string.h>
#include <utils/lib.h>
#include "build_internal.h"
#include "../utils/whereami.h"

extern int llvm_version_major;
Expand All @@ -24,30 +15,30 @@ extern const char* llvm_version;
extern const char* llvm_target;

char *arch_os_target[ARCH_OS_TARGET_LAST + 1] = {
[ELF_AARCH64] = "elf-aarch64",
[ELF_RISCV32] = "elf-riscv32",
[ELF_RISCV64] = "elf-riscv64",
[ELF_X86] = "elf-x86",
[ELF_X64] = "elf-x64",
[FREEBSD_X86] = "freebsd-x86",
[FREEBSD_X64] = "freebsd-x64",
[LINUX_AARCH64] = "linux-aarch64",
[LINUX_RISCV32] = "linux-riscv32",
[LINUX_RISCV64] = "linux-riscv64",
[LINUX_X86] = "linux-x86",
[LINUX_X64] = "linux-x64",
[MACOS_AARCH64] = "macos-aarch64",
[MACOS_X64] = "macos-x64",
[MCU_X86] = "mcu-x86",
[MINGW_X64] = "mingw-x64",
[NETBSD_X86] = "netbsd-x86",
[NETBSD_X64] = "netbsd-x64",
[OPENBSD_X86] = "openbsd-x86",
[OPENBSD_X64] = "openbsd-x64",
[WASM32] = "wasm32",
[WASM64] = "wasm64",
[WINDOWS_AARCH64] = "windows-aarch64",
[WINDOWS_X64] = "windows-x64",
[ELF_AARCH64] = "elf-aarch64",
[ELF_RISCV32] = "elf-riscv32",
[ELF_RISCV64] = "elf-riscv64",
[ELF_X86] = "elf-x86",
[ELF_X64] = "elf-x64",
[FREEBSD_X86] = "freebsd-x86",
[FREEBSD_X64] = "freebsd-x64",
[LINUX_AARCH64] = "linux-aarch64",
[LINUX_RISCV32] = "linux-riscv32",
[LINUX_RISCV64] = "linux-riscv64",
[LINUX_X86] = "linux-x86",
[LINUX_X64] = "linux-x64",
[MACOS_AARCH64] = "macos-aarch64",
[MACOS_X64] = "macos-x64",
[MCU_X86] = "mcu-x86",
[MINGW_X64] = "mingw-x64",
[NETBSD_X86] = "netbsd-x86",
[NETBSD_X64] = "netbsd-x64",
[OPENBSD_X86] = "openbsd-x86",
[OPENBSD_X64] = "openbsd-x64",
[WASM32] = "wasm32",
[WASM64] = "wasm64",
[WINDOWS_AARCH64] = "windows-aarch64",
[WINDOWS_X64] = "windows-x64",
};

#define EOUTPUT(string, ...) fprintf(stderr, string "\n", ##__VA_ARGS__)
Expand Down Expand Up @@ -404,29 +395,40 @@ static void add_linker_arg(BuildOptions *options, const char *arg)
options->linker_args[options->linker_arg_count++] = arg;
}

/**
* Update feature flags, adding to one list and removing it from the other.
* @param flags the "add" flags
* @param removed_flags the "undef" flags
* @param arg the argument to add or undef
* @param add true if we add, false to undef
*/
void update_feature_flags(const char ***flags, const char ***removed_flags, const char *arg, bool add)
{
const char **to_remove_from = add ? *removed_flags : *flags;
unsigned len = vec_size(to_remove_from);
// Remove if it's in there.
for (unsigned i = 0; i < len; i++)
{
if (str_eq(to_remove_from[i], arg))
// We keep two lists "remove" and "add" lists:
const char ***to_remove_from = add ? removed_flags : flags;

// Remove from opposite list using string equality
// More elegant would be using a Set or Map, but that's overkill
// for something that's likely just 1-2 values.
FOREACH_BEGIN_IDX(i, const char *value, *to_remove_from)
if (str_eq(value, arg))
{
vec_erase_ptr_at(to_remove_from, i);
vec_erase_ptr_at(*to_remove_from, i);
break;
}
}
FOREACH_END();

// First we check that it's not in the list
const char ***to_add_to_ref = add ? flags : removed_flags;
unsigned add_len = vec_size(*to_add_to_ref);
for (unsigned i = 0; i < add_len; i++)
{
if (str_eq((*to_add_to_ref)[i], arg)) return;
}
FOREACH_BEGIN(const char *value, *to_add_to_ref)
// If we have a match, we don't add it.
if (str_eq(value, arg)) return;
FOREACH_END();

// No match, so add it.
vec_add(*to_add_to_ref, arg);
}


static int parse_multi_option(const char *start, unsigned count, const char** elements)
{
const char *arg = current_arg;
Expand Down Expand Up @@ -1062,7 +1064,7 @@ BuildOptions parse_arguments(int argc, const char *argv[])
parse_command(&build_options);
continue;
}
if (command_is_projectless(build_options.command) || build_options.command == COMMAND_GENERATE_HEADERS)
if (command_accepts_files(build_options.command) || build_options.command == COMMAND_GENERATE_HEADERS)
{
append_file(&build_options);
continue;
Expand Down
Loading

0 comments on commit 709fe1c

Please sign in to comment.