summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2015-06-23 09:47:37 +0800
committerruki <[email protected]>2015-06-23 09:47:37 +0800
commita49bed2da5116dde09989a91fc04e9c37fb11740 (patch)
treecfc91562f0321ebef77851df9444e810e312ff8c
parent6faa822ae876cd75a0ad936e9af863f559e9d8e8 (diff)
modify shared library template
-rw-r--r--xmake/scripts/base/config.lua30
-rw-r--r--xmake/scripts/tools/gcc.lua4
-rw-r--r--xmake/templates/c++/shared_library/project/src/interface.h11
-rw-r--r--xmake/templates/c++/shared_library/project/src/test.cpp10
-rw-r--r--xmake/templates/c++/shared_library/project/xmake.lua20
-rw-r--r--xmake/templates/c/shared_library/project/src/interface.h10
-rw-r--r--xmake/templates/c/shared_library/project/src/test.c8
-rw-r--r--xmake/templates/c/shared_library/project/xmake.lua20
8 files changed, 91 insertions, 22 deletions
diff --git a/xmake/scripts/base/config.lua b/xmake/scripts/base/config.lua
index d03a98c93..d6f6d9625 100644
--- a/xmake/scripts/base/config.lua
+++ b/xmake/scripts/base/config.lua
@@ -31,19 +31,14 @@ local option = require("base/option")
local global = require("base/global")
-- make configure for the current target
-function config._make()
+function config._make(configs)
- -- the configs
- local configs = config._CONFIGS
- assert(configs)
-
-- the options
local options = xmake._OPTIONS
assert(options)
- -- init current config
- config._CURRENT = config._CURRENT or {}
- local current = config._CURRENT
+ -- init current target configure
+ local current = {}
-- get configs from the global configure first
if global._CURRENT then
@@ -66,12 +61,15 @@ function config._make()
local target_config = configs._TARGET[current.target]
if target_config then
- -- merge to the current config
+ -- merge it
for k, v in pairs(target_config) do
current[k] = v
end
end
end
+
+ -- ok?
+ return current
end
-- get the configure file
@@ -201,9 +199,11 @@ function config.load()
-- save configs
config._CONFIGS = configs
+ -- make the current target configs
+ local current = config._make(configs)
+
-- clear configs and mark as "rebuild" and "reconfig" if the host has been changed
- local target = config._target()
- if target and target.host ~= xmake._HOST then
+ if current and current.host ~= xmake._HOST then
-- clear configs and mark as "rebuild"
config._CONFIGS = { __rebuild = true }
@@ -215,21 +215,21 @@ function config.load()
end
-- clear configs and mark as "rebuild" if the plat has been changed
- if target and target.plat and options.plat and target.plat ~= options.plat then
+ if current and current.plat and options.plat and current.plat ~= options.plat then
-- clear configs and mark as "rebuild"
config._CONFIGS = { __rebuild = true }
end
-- mark as "rebuild" if the arch has been changed
- if target and target.arch and options.arch and target.arch ~= options.arch then
+ if current and current.arch and options.arch and current.arch ~= options.arch then
-- mark as "rebuild"
config._CONFIGS.__rebuild = true
end
-- mark as "rebuild" if the mode has been changed
- if target and target.mode and options.mode and target.mode ~= options.mode then
+ if current and current.mode and options.mode and current.mode ~= options.mode then
-- mark as "rebuild"
config._CONFIGS.__rebuild = true
@@ -309,7 +309,7 @@ function config.load()
end
-- make the current config
- config._make()
+ config._CURRENT = config._make(config._CONFIGS)
end
-- clear up and remove all auto values
diff --git a/xmake/scripts/tools/gcc.lua b/xmake/scripts/tools/gcc.lua
index d02384494..164dfae12 100644
--- a/xmake/scripts/tools/gcc.lua
+++ b/xmake/scripts/tools/gcc.lua
@@ -71,9 +71,9 @@ function gcc.init(self, name)
-- init shflags
if name:find("clang") then
- self.shflags = { "-dynamiclib" }
+ self.shflags = { "-dynamiclib", "-fPIC" }
else
- self.shflags = { "-shared -Wl,-soname" }
+ self.shflags = { "-shared -Wl,-soname", "-fPIC" }
end
-- suppress warning for the clang
diff --git a/xmake/templates/c++/shared_library/project/src/interface.h b/xmake/templates/c++/shared_library/project/src/interface.h
index 99b4d38db..ef24ba3ce 100644
--- a/xmake/templates/c++/shared_library/project/src/interface.h
+++ b/xmake/templates/c++/shared_library/project/src/interface.h
@@ -2,6 +2,14 @@
extern "C" {
#endif
+#if defined(_WIN32)
+# define __export __declspec(dllexport)
+#elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
+# define __export __attribute__((visibility("default")))
+#else
+# define __export
+#endif
+
/*! calculate add(a, b)
*
* @param a the first argument
@@ -9,9 +17,8 @@ extern "C" {
*
* @return the result
*/
-int add(int a, int b);
+__export int add(int a, int b);
#ifdef __cplusplus
}
#endif
-
diff --git a/xmake/templates/c++/shared_library/project/src/test.cpp b/xmake/templates/c++/shared_library/project/src/test.cpp
new file mode 100644
index 000000000..72e03272b
--- /dev/null
+++ b/xmake/templates/c++/shared_library/project/src/test.cpp
@@ -0,0 +1,10 @@
+#include "interface.h"
+#include <iostream>
+
+using namespace std;
+
+int main(int argc, char** argv)
+{
+ cout << "add(1, 2) = " << add(1, 2) << endl;
+ return 0;
+}
diff --git a/xmake/templates/c++/shared_library/project/xmake.lua b/xmake/templates/c++/shared_library/project/xmake.lua
index 2f6742621..b776097d8 100644
--- a/xmake/templates/c++/shared_library/project/xmake.lua
+++ b/xmake/templates/c++/shared_library/project/xmake.lua
@@ -28,5 +28,23 @@ add_target("[targetname]")
set_kind("shared")
-- add files
- add_files("src/*.cpp")
+ add_files("src/interface.cpp")
+
+-- add target
+add_target("test")
+
+ -- set kind
+ set_kind("binary")
+
+ -- add deps
+ add_deps("[targetname]")
+
+ -- add files
+ add_files("src/test.cpp")
+
+ -- add links
+ add_links("[targetname]")
+
+ -- add link directory
+ add_linkdirs("$(buildir)")
diff --git a/xmake/templates/c/shared_library/project/src/interface.h b/xmake/templates/c/shared_library/project/src/interface.h
index 0289cd562..7a3eb570a 100644
--- a/xmake/templates/c/shared_library/project/src/interface.h
+++ b/xmake/templates/c/shared_library/project/src/interface.h
@@ -1,3 +1,11 @@
+#if defined(_WIN32)
+# define __export __declspec(dllexport)
+#elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
+# define __export __attribute__((visibility("default")))
+#else
+# define __export
+#endif
+
/*! calculate add(a, b)
*
* @param a the first argument
@@ -5,6 +13,6 @@
*
* @return the result
*/
-int add(int a, int b);
+__export int add(int a, int b);
diff --git a/xmake/templates/c/shared_library/project/src/test.c b/xmake/templates/c/shared_library/project/src/test.c
new file mode 100644
index 000000000..9505a2f75
--- /dev/null
+++ b/xmake/templates/c/shared_library/project/src/test.c
@@ -0,0 +1,8 @@
+#include "interface.h"
+#include <stdio.h>
+
+int main(int argc, char** argv)
+{
+ printf("add(1, 2) = %d\n", add(1, 2));
+ return 0;
+}
diff --git a/xmake/templates/c/shared_library/project/xmake.lua b/xmake/templates/c/shared_library/project/xmake.lua
index 07451d4bb..f6d15d851 100644
--- a/xmake/templates/c/shared_library/project/xmake.lua
+++ b/xmake/templates/c/shared_library/project/xmake.lua
@@ -28,5 +28,23 @@ add_target("[targetname]")
set_kind("shared")
-- add files
- add_files("src/*.c")
+ add_files("src/interface.c")
+
+-- add target
+add_target("test")
+
+ -- set kind
+ set_kind("binary")
+
+ -- add deps
+ add_deps("[targetname]")
+
+ -- add files
+ add_files("src/test.c")
+
+ -- add links
+ add_links("[targetname]")
+
+ -- add link directory
+ add_linkdirs("$(buildir)")