From 36352085ae880388b0c577c99f26dfb43aed601e Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 23 Jul 2020 06:58:44 +0200 Subject: [PATCH] v: add support for iOS crosscompilation (#5943) --- cmd/v/help/build-c.txt | 4 ++-- vlib/builtin/cfns.c.v | 2 -- vlib/os/os.v | 17 +++++++++++++++-- vlib/v/builder/cc.v | 6 ++++++ vlib/v/gen/cgen.v | 6 ++++++ vlib/v/parser/comptime.v | 5 ++++- vlib/v/pref/os.v | 12 ++++++++++++ vlib/v/pref/pref.v | 2 +- 8 files changed, 46 insertions(+), 8 deletions(-) diff --git a/cmd/v/help/build-c.txt b/cmd/v/help/build-c.txt index 897fbd7f68..1063426271 100644 --- a/cmd/v/help/build-c.txt +++ b/cmd/v/help/build-c.txt @@ -49,8 +49,8 @@ These build flags are enabled on `build` and `run` as long as the backend is set Change the target OS that V tries to compile for. By default, the target OS is the host system. When OS is `cross`, V will attempt to output cross-platform C code. - List of OS supported by V: `linux`, `windows`, `mac`, `freebsd`, `openbsd`, `netbsd`, - `dragonfly`, `solaris`, `android` and `haiku`. + List of OS supported by V: `linux`, `windows`, `ios`, `mac`, `freebsd`, `openbsd`, + `netbsd`, `dragonfly`, `solaris`, `android` and `haiku`. -sanitize Pass flags related to sanitization to the C compiler. diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index da27376b52..a94cf1508d 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -67,10 +67,8 @@ fn C.fclose() int fn C.pclose() int - fn C.system() int - fn C.setenv(charptr) int diff --git a/vlib/os/os.v b/vlib/os/os.v index 4f53a0288d..cf88ac342f 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -453,9 +453,22 @@ pub fn system(cmd string) int { ret = C._wsystem(wcmd.to_wide()) } } $else { - unsafe { - ret = C.system(charptr(cmd.str)) +/* + // make + // make selfcompile + // ./v -os ios hello.v + $if ios { + // TODO: use dlsym, use posix_spawn or embed ios_system + eprintln('system not supported on ios') + ret = 1 + } $else { +*/ + unsafe { + ret = C.system(charptr(cmd.str)) + } +/* } +*/ } if ret == -1 { print_c_errno() diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index cb82fdbcd2..0dde6f2e0a 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -136,6 +136,9 @@ fn (mut v Builder) cc() { return } } + if v.pref.os == .ios { + ccompiler = 'xcrun --sdk iphoneos gcc -arch arm64' + } // arguments for the C compiler // TODO : activate -Werror once no warnings remain // '-Werror', @@ -339,6 +342,9 @@ fn (mut v Builder) cc() { if v.pref.os == .mac { a << '-mmacosx-version-min=10.7' } + if v.pref.os == .ios { + a << '-miphoneos-version-min=10.0' + } if v.pref.os == .windows { a << '-municode' } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 7508a7c1c6..5ba61ff89e 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -253,6 +253,9 @@ pub fn (mut g Gen) init() { g.definitions.writeln('string _STR(const char*, int, ...);') g.definitions.writeln('string _STR_TMP(const char*, ...);') } + if g.pref.os == .ios { + g.cheaders.writeln('#define __TARGET_IOS__ 1') + } g.write_builtin_types() g.write_typedef_types() g.write_typeof_functions() @@ -3901,6 +3904,9 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string { 'windows' { return '_WIN32' } + 'ios' { + return '__TARGET_IOS__' + } 'mac' { return '__APPLE__' } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index c7151e8e1c..a2d7770275 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -28,7 +28,7 @@ fn (mut p Parser) resolve_vroot(flag string) string { vmod_path := vmod_file_location.vmod_folder if p.pref.is_fmt { return flag - } + } return flag.replace('@VROOT', os.real_path(vmod_path)) } @@ -271,6 +271,9 @@ fn os_from_string(os string) pref.OS { 'windows' { return .windows } + 'ios' { + return .ios + } 'mac' { return .mac } diff --git a/vlib/v/pref/os.v b/vlib/v/pref/os.v index e1a9296dff..02b66e9174 100644 --- a/vlib/v/pref/os.v +++ b/vlib/v/pref/os.v @@ -5,6 +5,7 @@ module pref pub enum OS { _auto // Reserved so .mac cannot be misunderstood as auto + ios mac linux windows @@ -27,6 +28,9 @@ pub fn os_from_string(os_str string) ?OS { 'windows' { return .windows } + 'ios' { + return .ios + } 'mac' { return .mac } @@ -74,6 +78,9 @@ pub fn (o OS) str() string { ._auto { return 'RESERVED: AUTO' } + .ios { + return 'iOS' + } .mac { return 'MacOS' } @@ -114,6 +121,11 @@ pub fn get_host_os() OS { $if linux { return .linux } +/* + $if ios { + return .ios + } +*/ $if macos { return .mac } diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 5b382995aa..2556127ba3 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -339,7 +339,7 @@ fn must_exist(path string) { if !os.exists(path) { eprintln('v expects that `$path` exists, but it does not') exit(1) - } + } } pub fn backend_from_string(s string) ?Backend {