From b3f129d3137855271bd929f4d6df7e51cf99f7ac Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 30 Jun 2016 10:17:13 +0800 Subject: fix install config header and update api for template --- README.md | 7 - xmake/actions/clean/main.lua | 2 +- xmake/actions/config/config_h.lua | 156 --------------------- xmake/actions/config/configheader.lua | 156 +++++++++++++++++++++ xmake/actions/config/main.lua | 4 +- xmake/actions/package/main.lua | 6 +- xmake/core/project/target.lua | 46 +++++- xmake/platforms/macosx/installer.lua | 12 +- xmake/templates/c++/console_tbox/project/xmake.lua | 2 +- .../c++/shared_library_tbox/project/xmake.lua | 2 +- .../c++/static_library_tbox/project/xmake.lua | 2 +- xmake/templates/c/console_tbox/project/xmake.lua | 2 +- .../c/shared_library_tbox/project/xmake.lua | 2 +- .../c/static_library_tbox/project/xmake.lua | 2 +- 14 files changed, 217 insertions(+), 184 deletions(-) delete mode 100755 xmake/actions/config/config_h.lua create mode 100755 xmake/actions/config/configheader.lua diff --git a/README.md b/README.md index cc4989e52..64dfdf1bd 100755 --- a/README.md +++ b/README.md @@ -375,13 +375,6 @@ xmake的目标是开发者更加关注于项目本身开发,简化项目的描 * [libsvx](https://github.com/caikelun/libsvx) * [更多项目](https://github.com/waruqi/xmake/wiki/%E4%BD%BF%E7%94%A8xmake%E7%9A%84%E5%BC%80%E6%BA%90%E5%BA%93) -####后续工作 - -1. 实现生成.ipa、.apk、.app、.deb、.rmp的打包插件 -2. 实现包的自动依赖管理,如果依赖包不存在,会去自动下载编译安装后,继续进行构建,支持交叉平台的包管理 -3. 实现转换automake、cmake的工到xmake的描述文件的插件,实现无缝编译 -4. 实现vs、xcode等工程文件生成插件 - ####简单例子 -- the debug mode diff --git a/xmake/actions/clean/main.lua b/xmake/actions/clean/main.lua index ba4330d40..040b43f8c 100755 --- a/xmake/actions/clean/main.lua +++ b/xmake/actions/clean/main.lua @@ -74,7 +74,7 @@ function _on_clean_target(target) if option.get("all") then -- remove the config.h file - _remove(target:get("config_h")) + _remove(target:configheader()) end end diff --git a/xmake/actions/config/config_h.lua b/xmake/actions/config/config_h.lua deleted file mode 100755 index 585e651d5..000000000 --- a/xmake/actions/config/config_h.lua +++ /dev/null @@ -1,156 +0,0 @@ ---!The Automatic Cross-platform Build Tool --- --- XMake is free software; you can redistribute it and/or modify --- it under the terms of the GNU Lesser General Public License as published by --- the Free Software Foundation; either version 2.1 of the License, or --- (at your option) any later version. --- --- XMake is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU Lesser General Public License for more details. --- --- You should have received a copy of the GNU Lesser General Public License --- along with XMake; --- If not, see http://www.gnu.org/licenses/ --- --- Copyright (C) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file config_h.lua --- - --- imports -import("core.base.option") -import("core.project.config") -import("core.project.project") - --- make configure for the given target name -function _make_for_target(files, target) - - -- get the target configure file - local config_h = target:get("config_h") - if not config_h then return end - - -- the prefix - local prefix = target:get("config_h_prefix") or (target:name():upper() .. "_CONFIG") - - -- open the file - local file = files[config_h] or io.open(config_h, "w") - - -- make the head - if files[config_h] then file:print("") end - file:print("#ifndef %s_H", prefix) - file:print("#define %s_H", prefix) - file:print("") - - -- make version - local version = target:get("version") - if version then - file:print("// version") - file:print("#define %s_VERSION \"%s\"", prefix, version) - local i = 1 - local m = {"MAJOR", "MINOR", "ALTER"} - for v in version:gmatch("%d+") do - file:print("#define %s_VERSION_%s %s", prefix, m[i], v) - i = i + 1 - if i > 3 then break end - end - file:print("#define %s_VERSION_BUILD %s", prefix, os.date("%Y%m%d%H%M", os.time())) - file:print("") - end - - -- make the defines - local defines = table.copy(target:get("defines_h")) - - -- make the undefines - local undefines = table.copy(target:get("undefines_h")) - - -- make the options - for name, opt in pairs(target:options()) do - - -- get the option defines - table.join2(defines, opt:get("defines_h_if_ok")) - - -- get the option undefines - table.join2(undefines, opt:get("undefines_h_if_ok")) - end - - -- make the defines - if #defines ~= 0 then - file:print("// defines") - for _, define in ipairs(defines) do - if define:find("=") then - file:print("#define %s", define:gsub("=", " "):gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end)) - else - file:print("#define %s 1", define:gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end)) - end - end - file:print("") - end - - -- make the undefines - if #undefines ~= 0 then - file:print("// undefines") - for _, undefine in ipairs(undefines) do - file:print("#undef %s", undefine:gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end)) - end - file:print("") - end - - -- make the tail - file:print("#endif") - - -- cache the file - files[config_h] = file - -end - --- make the configure file for the given target and dependents -function _make_for_target_with_deps(files, targetname) - - -- the target - local target = project.target(targetname) - - -- make configure for the target - _make_for_target(files, target) - - -- make configure for the dependent targets? - for _, dep in ipairs(target:get("deps")) do - _make_for_target_with_deps(files, dep) - end - -end - --- make the config.h -function make() - - -- the target name - local targetname = option.get("target") - - -- enter project directory - os.cd(project.directory()) - - -- init files - local files = {} - - -- make configure for the given target name - if targetname and targetname ~= "all" then - _make_for_target_with_deps(files, targetname) - else - - -- make configure for all targets - for _, target in pairs(project.targets()) do - _make_for_target(files, target) - end - end - - -- exit files - for _, file in pairs(files) do - file:close() - end - - -- leave project directory - os.cd("-") - -end diff --git a/xmake/actions/config/configheader.lua b/xmake/actions/config/configheader.lua new file mode 100755 index 000000000..0871e5909 --- /dev/null +++ b/xmake/actions/config/configheader.lua @@ -0,0 +1,156 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see http://www.gnu.org/licenses/ +-- +-- Copyright (C) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file configheader.lua +-- + +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.project") + +-- make configure for the given target name +function _make_for_target(files, target) + + -- get the target configure file + local configheader = target:configheader() + if not configheader then return end + + -- the prefix + local prefix = target:get("config_h_prefix") or (target:name():upper() .. "_CONFIG") + + -- open the file + local file = files[configheader] or io.open(configheader, "w") + + -- make the head + if files[configheader] then file:print("") end + file:print("#ifndef %s_H", prefix) + file:print("#define %s_H", prefix) + file:print("") + + -- make version + local version = target:get("version") + if version then + file:print("// version") + file:print("#define %s_VERSION \"%s\"", prefix, version) + local i = 1 + local m = {"MAJOR", "MINOR", "ALTER"} + for v in version:gmatch("%d+") do + file:print("#define %s_VERSION_%s %s", prefix, m[i], v) + i = i + 1 + if i > 3 then break end + end + file:print("#define %s_VERSION_BUILD %s", prefix, os.date("%Y%m%d%H%M", os.time())) + file:print("") + end + + -- make the defines + local defines = table.copy(target:get("defines_h")) + + -- make the undefines + local undefines = table.copy(target:get("undefines_h")) + + -- make the options + for name, opt in pairs(target:options()) do + + -- get the option defines + table.join2(defines, opt:get("defines_h_if_ok")) + + -- get the option undefines + table.join2(undefines, opt:get("undefines_h_if_ok")) + end + + -- make the defines + if #defines ~= 0 then + file:print("// defines") + for _, define in ipairs(defines) do + if define:find("=") then + file:print("#define %s", define:gsub("=", " "):gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end)) + else + file:print("#define %s 1", define:gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end)) + end + end + file:print("") + end + + -- make the undefines + if #undefines ~= 0 then + file:print("// undefines") + for _, undefine in ipairs(undefines) do + file:print("#undef %s", undefine:gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end)) + end + file:print("") + end + + -- make the tail + file:print("#endif") + + -- cache the file + files[configheader] = file + +end + +-- make the configure file for the given target and dependents +function _make_for_target_with_deps(files, targetname) + + -- the target + local target = project.target(targetname) + + -- make configure for the target + _make_for_target(files, target) + + -- make configure for the dependent targets? + for _, dep in ipairs(target:get("deps")) do + _make_for_target_with_deps(files, dep) + end + +end + +-- make the config.h +function make() + + -- the target name + local targetname = option.get("target") + + -- enter project directory + os.cd(project.directory()) + + -- init files + local files = {} + + -- make configure for the given target name + if targetname and targetname ~= "all" then + _make_for_target_with_deps(files, targetname) + else + + -- make configure for all targets + for _, target in pairs(project.targets()) do + _make_for_target(files, target) + end + end + + -- exit files + for _, file in pairs(files) do + file:close() + end + + -- leave project directory + os.cd("-") + +end diff --git a/xmake/actions/config/main.lua b/xmake/actions/config/main.lua index b042793af..0144ee3c6 100755 --- a/xmake/actions/config/main.lua +++ b/xmake/actions/config/main.lua @@ -27,7 +27,7 @@ import("core.project.global") import("core.project.project") import("core.platform.platform") import("core.project.cache") -import("config_h") +import("configheader") -- filter option function _option_filter(name) @@ -185,7 +185,7 @@ function main() cache.flush() -- make the config.h - config_h.make() + configheader.make() -- dump it config.dump() diff --git a/xmake/actions/package/main.lua b/xmake/actions/package/main.lua index 6128eec15..986d28d17 100755 --- a/xmake/actions/package/main.lua +++ b/xmake/actions/package/main.lua @@ -41,9 +41,9 @@ function _package_library(target) os.cp(target:targetfile(), format("%s/%s.pkg/lib/$(mode)/$(plat)/$(arch)/%s", outputdir, targetname, path.filename(target:targetfile()))) -- copy the config.h to the output directory - local config_h = target:get("config_h") - if config_h then - os.cp(config_h, format("%s/%s.pkg/inc/$(plat)/%s", outputdir, targetname, path.filename(config_h))) + local configheader = target:configheader() + if configheader then + os.cp(configheader, format("%s/%s.pkg/inc/$(plat)/%s", outputdir, targetname, path.filename(configheader))) end -- copy headers diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 991816e96..a1f75be5f 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -139,6 +139,38 @@ function target:targetfile() return path.join(targetdir, filename) end +-- get the config header files +function target:configheader(outputdir) + + -- get config header + local configheader = self:get("config_h") + if not configheader then + return + end + + -- get the root directory + local rootdir, count = configheader:gsub("|.*$", ""):gsub("%(.*%)$", "") + if count == 0 then + rootdir = nil + end + + -- remove '(' and ')' + configheader = configheader:gsub("[%(%)]", "") + + -- get the output header + local outputheader = nil + if outputdir then + if rootdir then + outputheader = path.absolute(path.relative(configheader, rootdir), outputdir) + else + outputheader = path.join(outputdir, path.filename(configheader)) + end + end + + -- ok + return configheader, outputheader +end + -- get the source files function target:sourcefiles() @@ -302,7 +334,10 @@ function target:headerfiles(outputdir) for _, header in ipairs(table.wrap(headers)) do -- get the root directory - local rootdir = header:gsub("%(.*%)", ""):gsub("|.*$", "") + local rootdir, count = header:gsub("|.*$", ""):gsub("%(.*%)$", "") + if count == 0 then + rootdir = nil + end -- remove '(' and ')' local srcpathes = header:gsub("[%(%)]", "") @@ -318,8 +353,13 @@ function target:headerfiles(outputdir) -- add the destinate headers for _, srcpath in ipairs(srcpathes) do - -- the header - local dstheader = path.absolute(path.relative(srcpath, rootdir), headerdir) + -- the destinate header + local dstheader = nil + if rootdir then + dstheader = path.absolute(path.relative(srcpath, rootdir), headerdir) + else + dstheader = path.join(headerdir, path.filename(srcpath)) + end assert(dstheader) -- add it diff --git a/xmake/platforms/macosx/installer.lua b/xmake/platforms/macosx/installer.lua index 426adc832..9544749d1 100644 --- a/xmake/platforms/macosx/installer.lua +++ b/xmake/platforms/macosx/installer.lua @@ -62,9 +62,9 @@ function _install_library(target) os.cp(target:targetfile(), librarydir) -- copy the config.h to the include directory - local config_h = target:get("config_h") - if config_h then - os.cp(config_h, includedir) + local configheader, configoutput = target:configheader(includedir) + if configheader and configoutput then + os.cp(configheader, configoutput) end -- copy headers to the include directory @@ -110,9 +110,9 @@ function _uninstall_library(target) os.rm(path.join(librarydir, path.filename(target:targetfile()))) -- reove the config.h from the include directory - local config_h = target:get("config_h") - if config_h then - os.rm(path.join(includedir, path.filename(config_h))) + local _, configoutput = target:configheader(includedir) + if configoutput then + os.rm(configoutput) end -- remove headers from the include directory diff --git a/xmake/templates/c++/console_tbox/project/xmake.lua b/xmake/templates/c++/console_tbox/project/xmake.lua index 048bb99ba..9fc47c0ea 100644 --- a/xmake/templates/c++/console_tbox/project/xmake.lua +++ b/xmake/templates/c++/console_tbox/project/xmake.lua @@ -105,7 +105,7 @@ if is_plat("windows") then end -- add packages -add_pkgdirs("pkg") +add_packagedirs("pkg") -- add projects add_subdirs("src") diff --git a/xmake/templates/c++/shared_library_tbox/project/xmake.lua b/xmake/templates/c++/shared_library_tbox/project/xmake.lua index 808c17645..d94875110 100644 --- a/xmake/templates/c++/shared_library_tbox/project/xmake.lua +++ b/xmake/templates/c++/shared_library_tbox/project/xmake.lua @@ -112,7 +112,7 @@ option("demo") set_option_description("Enable or disable the demo module") -- add packages -add_pkgdirs("pkg") +add_packagedirs("pkg") -- add projects add_subdirs("src/[targetname]") diff --git a/xmake/templates/c++/static_library_tbox/project/xmake.lua b/xmake/templates/c++/static_library_tbox/project/xmake.lua index 808c17645..d94875110 100644 --- a/xmake/templates/c++/static_library_tbox/project/xmake.lua +++ b/xmake/templates/c++/static_library_tbox/project/xmake.lua @@ -112,7 +112,7 @@ option("demo") set_option_description("Enable or disable the demo module") -- add packages -add_pkgdirs("pkg") +add_packagedirs("pkg") -- add projects add_subdirs("src/[targetname]") diff --git a/xmake/templates/c/console_tbox/project/xmake.lua b/xmake/templates/c/console_tbox/project/xmake.lua index 048bb99ba..9fc47c0ea 100644 --- a/xmake/templates/c/console_tbox/project/xmake.lua +++ b/xmake/templates/c/console_tbox/project/xmake.lua @@ -105,7 +105,7 @@ if is_plat("windows") then end -- add packages -add_pkgdirs("pkg") +add_packagedirs("pkg") -- add projects add_subdirs("src") diff --git a/xmake/templates/c/shared_library_tbox/project/xmake.lua b/xmake/templates/c/shared_library_tbox/project/xmake.lua index 808c17645..d94875110 100644 --- a/xmake/templates/c/shared_library_tbox/project/xmake.lua +++ b/xmake/templates/c/shared_library_tbox/project/xmake.lua @@ -112,7 +112,7 @@ option("demo") set_option_description("Enable or disable the demo module") -- add packages -add_pkgdirs("pkg") +add_packagedirs("pkg") -- add projects add_subdirs("src/[targetname]") diff --git a/xmake/templates/c/static_library_tbox/project/xmake.lua b/xmake/templates/c/static_library_tbox/project/xmake.lua index 808c17645..d94875110 100644 --- a/xmake/templates/c/static_library_tbox/project/xmake.lua +++ b/xmake/templates/c/static_library_tbox/project/xmake.lua @@ -112,7 +112,7 @@ option("demo") set_option_description("Enable or disable the demo module") -- add packages -add_pkgdirs("pkg") +add_packagedirs("pkg") -- add projects add_subdirs("src/[targetname]") -- cgit v1.3.1