From 28fb2403d9e37fc0a2a44aeacfdc67d1387853e5 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 18 Aug 2019 10:00:04 -0500 Subject: [PATCH] Add os/arch to core. Also allow setting custom keywords for compiled os name and architecture name. --- auxlib/cook.janet | 1 + meson.build | 6 ++++++ meson_options.txt | 3 +++ src/conf/janetconf.h | 2 ++ src/core/os.c | 45 ++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/auxlib/cook.janet b/auxlib/cook.janet index 48fa3362..8ad2bf13 100644 --- a/auxlib/cook.janet +++ b/auxlib/cook.janet @@ -555,6 +555,7 @@ int main(int argc, const char **argv) { (os/cd module-dir) (try (with-dyns [:rules @{}] + (os/execute ["git" "submodule" "update" "--init" "--recursive"]) (import-rules "./project.janet") (do-rule "install-deps") (do-rule "build") diff --git a/meson.build b/meson.build index 0ca433c8..02c71ee7 100644 --- a/meson.build +++ b/meson.build @@ -65,6 +65,12 @@ conf.set('JANET_RECURSION_GUARD', get_option('recursion_guard')) conf.set('JANET_MAX_PROTO_DEPTH', get_option('max_proto_depth')) conf.set('JANET_MAX_MACRO_EXPAND', get_option('max_macro_expand')) conf.set('JANET_STACK_MAX', get_option('stack_max')) +if get_option('os_name') != '' + conf.set('JANET_OS_NAME', get_option('os_name')) +endif +if get_option('arch_name') != '' + conf.set('JANET_ARCH_NAME', get_option('arch_name')) +endif jconf = configure_file(output : 'janetconf.h', configuration : conf) diff --git a/meson_options.txt b/meson_options.txt index ba917145..afa8f0b9 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -15,3 +15,6 @@ option('recursion_guard', type : 'integer', min : 10, max : 8000, value : 1024) option('max_proto_depth', type : 'integer', min : 10, max : 8000, value : 200) option('max_macro_expand', type : 'integer', min : 1, max : 8000, value : 200) option('stack_max', type : 'integer', min : 8096, max : 1000000000, value : 16384) + +option('arch_name', type : 'string', value: '') +option('os_name', type : 'string', value: '') diff --git a/src/conf/janetconf.h b/src/conf/janetconf.h index a16194e6..3e0cb05f 100644 --- a/src/conf/janetconf.h +++ b/src/conf/janetconf.h @@ -56,5 +56,7 @@ /* #define JANET_MAX_PROTO_DEPTH 200 */ /* #define JANET_MAX_MACRO_EXPAND 200 */ /* #define JANET_STACK_MAX 16384 */ +/* #define JANET_OS_NAME my-custom-os */ +/* #define JANET_ARCH_NAME pdp-8 */ #endif /* end of include guard: JANETCONF_H */ diff --git a/src/core/os.c b/src/core/os.c index 143e95b8..a2f2c658 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -64,12 +64,17 @@ extern char **environ; /* Full OS functions */ +#define janet_stringify1(x) #x +#define janet_stringify(x) janet_stringify1(x) + static Janet os_which(int32_t argc, Janet *argv) { janet_fixarity(argc, 0); (void) argv; -#ifdef JANET_WINDOWS +#if defined(JANET_OS_NAME) + return janet_ckeywordv(janet_stringify(JANET_OS_NAME)); +#elif defined(JANET_WINDOWS) return janet_ckeywordv("windows"); -#elif __APPLE__ +#elif defined(__APPLE__) return janet_ckeywordv("macos"); #elif defined(__EMSCRIPTEN__) return janet_ckeywordv("web"); @@ -86,6 +91,31 @@ static Janet os_which(int32_t argc, Janet *argv) { #endif } +/* Detect the ISA we are compiled for */ +static Janet os_arch(int32_t argc, Janet *argv) { + janet_fixarity(argc, 0); + (void) argv; + /* Check 64-bit vs 32-bit */ +#if defined(JANET_ARCH_NAME) + return janet_ckeywordv(janet_stringify(JANET_ARCH_NAME)); +#elif (defined(__x86_64__) || defined(_M_X64)) + return janet_ckeywordv("x86-64"); +#elif defined(__i386) || defined(_M_IX86) + return janet_ckeywordv("x86"); +#elif defined(_M_ARM64) || defined(__aarch64__) + return janet_ckeywordv("aarch64"); +#elif defined(_M_ARM) || defined(__arm__) + return janet_ckeywordv("arm"); +#elif (defined(__sparc__)) + return janet_ckeywordv("sparc"); +#else + return janet_ckeywordv("unknown"); +#endif +} + +#undef janet_stringify1 +#undef janet_stringify + static Janet os_exit(int32_t argc, Janet *argv) { janet_arity(argc, 0, 1); if (argc == 0) { @@ -783,6 +813,17 @@ static const JanetReg os_cfuns[] = { JDOC("(os/getenv variable)\n\n" "Get the string value of an environment variable.") }, + { + "os/arch", os_arch, + JDOC("(os/arch)\n\n" + "Check the ISA that janet was compiled for. Returns one of:\n\n" + "\t:x86\n" + "\t:x86-64\n" + "\t:arm\n" + "\t:aarch64\n" + "\t:sparc\n" + "\t:unknown\n") + }, #ifndef JANET_REDUCED_OS { "os/dir", os_dir,