From 2ce6b190dd7596e0906c7d4af64c96b9d29a26ac Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 6 Feb 2020 18:38:35 +0200 Subject: [PATCH] @VMODULE relative paths in #flag lines --- vlib/compiler/aparser.v | 2 ++ vlib/compiler/comptime.v | 1 + vlib/compiler/tests/project_with_c_code/.gitignore | 3 +++ vlib/compiler/tests/project_with_c_code/main.v | 8 ++++++++ vlib/compiler/tests/project_with_c_code/main_test.v | 5 +++++ .../tests/project_with_c_code/mod1/c/header.h | 6 ++++++ .../project_with_c_code/mod1/c/implementation.c | 5 +++++ .../tests/project_with_c_code/mod1/wrapper.v | 12 ++++++++++++ 8 files changed, 42 insertions(+) create mode 100644 vlib/compiler/tests/project_with_c_code/.gitignore create mode 100644 vlib/compiler/tests/project_with_c_code/main.v create mode 100644 vlib/compiler/tests/project_with_c_code/main_test.v create mode 100644 vlib/compiler/tests/project_with_c_code/mod1/c/header.h create mode 100644 vlib/compiler/tests/project_with_c_code/mod1/c/implementation.c create mode 100644 vlib/compiler/tests/project_with_c_code/mod1/wrapper.v diff --git a/vlib/compiler/aparser.v b/vlib/compiler/aparser.v index cbaf314807..b3ad571158 100644 --- a/vlib/compiler/aparser.v +++ b/vlib/compiler/aparser.v @@ -14,6 +14,7 @@ import ( struct Parser { file_path string // "/home/user/hello.v" + file_path_dir string // "/home/user" file_name string // "hello.v" file_platform string // ".v", "_windows.v", "_nix.v", "_darwin.v", "_linux.v" ... // When p.file_pcguard != '', it contains a @@ -180,6 +181,7 @@ fn (v mut V) new_parser_from_file(path string) Parser { p = { p | file_path:path, + file_path_dir:filepath.dir( path ), file_name:path.all_after(os.path_separator), file_platform:path_platform, file_pcguard:path_pcguard, diff --git a/vlib/compiler/comptime.v b/vlib/compiler/comptime.v index b54278f160..5ecff496ab 100644 --- a/vlib/compiler/comptime.v +++ b/vlib/compiler/comptime.v @@ -243,6 +243,7 @@ fn (p mut Parser) chash() { if p.first_pass() { mut flag := hash[5..] // expand `@VROOT` `@VMOD` to absolute path + flag = flag.replace('@VMODULE', p.file_path_dir) flag = flag.replace('@VROOT', p.vroot) flag = flag.replace('@VPATH', p.pref.vpath) flag = flag.replace('@VLIB_PATH', p.pref.vlib_path) diff --git a/vlib/compiler/tests/project_with_c_code/.gitignore b/vlib/compiler/tests/project_with_c_code/.gitignore new file mode 100644 index 0000000000..4607858382 --- /dev/null +++ b/vlib/compiler/tests/project_with_c_code/.gitignore @@ -0,0 +1,3 @@ +main +mod1/c/implementation.o +main_test diff --git a/vlib/compiler/tests/project_with_c_code/main.v b/vlib/compiler/tests/project_with_c_code/main.v new file mode 100644 index 0000000000..ef865602ea --- /dev/null +++ b/vlib/compiler/tests/project_with_c_code/main.v @@ -0,0 +1,8 @@ +module main + +import mod1 + +fn main(){ + res := mod1.vadd(1,2) + println( res ) +} diff --git a/vlib/compiler/tests/project_with_c_code/main_test.v b/vlib/compiler/tests/project_with_c_code/main_test.v new file mode 100644 index 0000000000..4dfd809e86 --- /dev/null +++ b/vlib/compiler/tests/project_with_c_code/main_test.v @@ -0,0 +1,5 @@ +import mod1 + +fn test_using_c_code_in_the_same_module_works(){ + assert 1003 == mod1.vadd(1,2) +} diff --git a/vlib/compiler/tests/project_with_c_code/mod1/c/header.h b/vlib/compiler/tests/project_with_c_code/mod1/c/header.h new file mode 100644 index 0000000000..f4e0ca6b86 --- /dev/null +++ b/vlib/compiler/tests/project_with_c_code/mod1/c/header.h @@ -0,0 +1,6 @@ +#ifndef ADD_H +#define ADD_H + +int cadd(int a, int b); + +#endif diff --git a/vlib/compiler/tests/project_with_c_code/mod1/c/implementation.c b/vlib/compiler/tests/project_with_c_code/mod1/c/implementation.c new file mode 100644 index 0000000000..03d7c94aad --- /dev/null +++ b/vlib/compiler/tests/project_with_c_code/mod1/c/implementation.c @@ -0,0 +1,5 @@ +#include "header.h" + +int cadd(int a, int b) { + return a + b; +} diff --git a/vlib/compiler/tests/project_with_c_code/mod1/wrapper.v b/vlib/compiler/tests/project_with_c_code/mod1/wrapper.v new file mode 100644 index 0000000000..f31d546a07 --- /dev/null +++ b/vlib/compiler/tests/project_with_c_code/mod1/wrapper.v @@ -0,0 +1,12 @@ +module mod1 + +#flag -I @VMODULE/c +#flag @VMODULE/c/implementation.o + +#include "header.h" + +fn C.cadd(int,int) int + +pub fn vadd(a int, b int) int { + return 1000 + C.cadd(a,b) +}