Add os/arch to core.

Also allow setting custom keywords for compiled
os name and architecture name.
This commit is contained in:
Calvin Rose 2019-08-18 10:00:04 -05:00
parent 1872bd344f
commit 28fb2403d9
5 changed files with 55 additions and 2 deletions

View File

@ -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")

View File

@ -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)

View File

@ -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: '')

View File

@ -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 */

View File

@ -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,