From 30401e003f21afb8dbefc48a4d7a05f828fff013 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 1 Jul 2022 12:48:31 +0300 Subject: [PATCH] v: support better separation of general Android vs Termux specific code (part 1) --- cmd/v/help/build-c.txt | 6 ++++-- vlib/os/os.v | 6 ++++++ vlib/v/checker/checker.v | 4 ++-- vlib/v/doc/doc.v | 2 ++ vlib/v/gen/c/comptime.v | 4 ++++ vlib/v/pref/os.v | 14 +++++++++++--- vlib/v/pref/should_compile.v | 32 ++++++++++++++++++++++++++++---- 7 files changed, 57 insertions(+), 11 deletions(-) diff --git a/cmd/v/help/build-c.txt b/cmd/v/help/build-c.txt index d8e48027a..9c44bfcd9 100644 --- a/cmd/v/help/build-c.txt +++ b/cmd/v/help/build-c.txt @@ -108,9 +108,11 @@ see also `v help build`. The compiler is known to also work, and has support for these operating systems also (although we do not test it as regularly as for the above): - `android`, `ios`, + `vinix`, + `ios`, + `android`, `termux`, `freebsd`, `openbsd`, `netbsd`, `dragonfly`, - `solaris`, `serenity`, `haiku`, `vinix`, + `solaris`, `serenity`, `haiku`, `wasm32`, `wasm32-wasi`, `wasm32-emscripten` Note that V has the concept of platform files, i.e. files ending diff --git a/vlib/os/os.v b/vlib/os/os.v index 891bf18c6..3d161dc57 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -317,6 +317,9 @@ pub fn get_raw_lines_joined() string { // user_os returns current user operating system name. pub fn user_os() string { $if linux { + if getenv('TERMUX_VERSION') != '' { + return 'termux' + } return 'linux' } $if macos { @@ -340,6 +343,9 @@ pub fn user_os() string { $if android { return 'android' } + // $if termux { + // return 'termux' + // } $if solaris { return 'solaris' } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7f372a7e5..c19b980a8 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -24,8 +24,8 @@ const ( pub const ( valid_comptime_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu', - 'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'solaris', - 'haiku', 'serenity', 'vinix'] + 'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'termux', + 'solaris', 'haiku', 'serenity', 'vinix'] valid_comptime_compression_types = ['none', 'zlib'] valid_comptime_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus'] valid_comptime_if_platforms = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32'] diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index 3f343709a..41cdf857c 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -39,6 +39,7 @@ pub enum Platform { dragonfly js // for interoperability in prefs.OS android + termux // like android, but note that termux is running on devices natively, not cross compiling from other platforms solaris serenity vinix @@ -64,6 +65,7 @@ pub fn platform_from_string(platform_str string) ?Platform { 'serenity' { return .serenity } 'vinix' { return .vinix } 'android' { return .android } + 'termux' { return .termux } 'haiku' { return .haiku } 'nix' { return .linux } '' { return .auto } diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 47bd9c183..3c449e9d9 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -636,6 +636,10 @@ fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_optional bool) ?str 'android' { return '__ANDROID__' } + 'termux' { + // Note: termux is running on Android natively + return '__ANDROID__' + } 'solaris' { return '__sun' } diff --git a/vlib/v/pref/os.v b/vlib/v/pref/os.v index 52b9dea71..efc1934c2 100644 --- a/vlib/v/pref/os.v +++ b/vlib/v/pref/os.v @@ -3,6 +3,8 @@ // that can be found in the LICENSE file. module pref +import os + pub enum OS { _auto // Reserved so .macos cannot be misunderstood as auto ios @@ -17,6 +19,7 @@ pub enum OS { js_browser js_freestanding android + termux // like android, but compiling/running natively on the devices solaris serenity vinix @@ -47,6 +50,7 @@ pub fn os_from_string(os_str string) ?OS { 'serenity' { return .serenity } 'vinix' { return .vinix } 'android' { return .android } + 'termux' { return .termux } 'haiku' { return .haiku } 'raw' { return .raw } 'nix' { return .linux } @@ -75,6 +79,7 @@ pub fn (o OS) str() string { .js_freestanding { return 'JavaScript' } .js_browser { return 'JavaScript(Browser)' } .android { return 'Android' } + .termux { return 'Termux' } .solaris { return 'Solaris' } .serenity { return 'SerenityOS' } .vinix { return 'Vinix' } @@ -88,10 +93,13 @@ pub fn (o OS) str() string { } pub fn get_host_os() OS { - $if android { - return .android - } $if linux { + $if android { + if os.getenv('TERMUX_VERSION') != '' { + return .termux + } + return .android + } return .linux } $if ios { diff --git a/vlib/v/pref/should_compile.v b/vlib/v/pref/should_compile.v index a7138b91d..657e4cfd9 100644 --- a/vlib/v/pref/should_compile.v +++ b/vlib/v/pref/should_compile.v @@ -121,6 +121,10 @@ fn fname_without_platform_postfix(file string) string { '_', 'android.c.v', '_', + 'termux.c.v', + '_', + 'android_outside_termux.c.v', + '_', 'freebsd.c.v', '_', 'openbsd.c.v', @@ -178,10 +182,6 @@ pub fn (prefs &Preferences) should_compile_c(file string) bool { if prefs.os != .ios && (file.ends_with('_ios.c.v') || file.ends_with('_ios.v')) { return false } - // - if prefs.os != .android && file.ends_with('_android.c.v') { - return false - } if prefs.os != .freebsd && file.ends_with('_freebsd.c.v') { return false } @@ -203,6 +203,30 @@ pub fn (prefs &Preferences) should_compile_c(file string) bool { if prefs.os != .vinix && file.ends_with('_vinix.c.v') { return false } + if prefs.os in [.android, .termux] { + // Note: Termux is running natively on Android devices, but the compilers there (clang) usually do not have access + // to the Android SDK. The code here ensures that you can have `_termux.c.v` and `_android_outside_termux.c.v` postfixes, + // to target both the cross compilation case (where the SDK headers are used and available), and the Termux case, + // where the Android SDK is not used. + if file.ends_with('_android.c.v') { + // common case, should compile for both cross android and termux + // eprintln('prefs.os: $prefs.os | file: $file | common') + return true + } + if file.ends_with('_android_outside_termux.c.v') { + // compile code that targets Android, but NOT Termux (i.e. the SDK is available) + // eprintln('prefs.os: $prefs.os | file: $file | android_outside_termux') + return prefs.os == .android + } + if file.ends_with('_termux.c.v') { + // compile Termux specific code + // eprintln('prefs.os: $prefs.os | file: $file | termux specific') + return prefs.os == .termux + } + } else if file.ends_with('_android.c.v') || file.ends_with('_termux.c.v') + || file.ends_with('_android_outside_termux.c.v') { + return false + } return true } -- 2.30.2