diff options
| author | ruki <[email protected]> | 2020-02-18 21:02:33 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-02-18 00:15:18 +0800 |
| commit | a3c18cbec05996bd9d2f9e85854109ad37a46f8d (patch) | |
| tree | 4ba91dd239d6a73dd4d48b70508724e24222ac3c | |
| parent | 531e766aff537bcd24ab0d92583f9099bc6f8567 (diff) | |
improve trybuild
| -rw-r--r-- | xmake/actions/build/main.lua | 32 | ||||
| -rw-r--r-- | xmake/actions/build/trybuild.lua | 155 | ||||
| -rw-r--r-- | xmake/actions/build/trybuild/autotools.lua | 45 | ||||
| -rw-r--r-- | xmake/actions/build/trybuild/cmake.lua | 52 | ||||
| -rw-r--r-- | xmake/actions/build/trybuild/make.lua | 35 | ||||
| -rw-r--r-- | xmake/actions/build/trybuild/msbuild.lua | 37 | ||||
| -rw-r--r-- | xmake/actions/build/trybuild/xcodebuild.lua | 37 | ||||
| -rw-r--r-- | xmake/actions/build/xmake.lua | 4 |
8 files changed, 238 insertions, 159 deletions
diff --git a/xmake/actions/build/main.lua b/xmake/actions/build/main.lua index 2002935b9..51813413b 100644 --- a/xmake/actions/build/main.lua +++ b/xmake/actions/build/main.lua @@ -27,15 +27,41 @@ import("core.platform.platform") import("build") import("build_files") import("cleaner") -import("trybuild") import("statistics") -- main function main() -- try building it using third-party buildsystem if xmake.lua not exists - if not os.isfile(project.file()) and option.get("try") then - return trybuild() + if not os.isfile(project.file()) then + + -- get the buildsystem tool + local configfile = nil + local tool = nil + local try_tool = option.get("try-tool") + if try_tool then + tool = import("trybuild." .. try_tool, {try = true, anonymous = true}) + if tool then + configfile = tool.detect() + else + raise("unknown build tool: %s", try_tool) + end + else + for _, name in ipairs({"msbuild", "xcodebuild", "cmake", "autotools", "make"}) do + tool = import("trybuild." .. name, {anonymous = true}) + configfile = tool.detect() + if configfile then + try_tool = name + break + end + end + end + + -- try building it + if configfile and tool and utils.confirm({default = true, description = "${bright}" .. path.filename(configfile) .. "${clear} found, try building it"}) then + tool.build() + end + return end -- post statistics before locking project diff --git a/xmake/actions/build/trybuild.lua b/xmake/actions/build/trybuild.lua deleted file mode 100644 index 5b59663ef..000000000 --- a/xmake/actions/build/trybuild.lua +++ /dev/null @@ -1,155 +0,0 @@ ---!A cross-platform build utility based on Lua --- --- Licensed under the Apache License, Version 2.0 (the "License"); --- you may not use this file except in compliance with the License. --- You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- --- Copyright (C) 2015-2020, TBOOX Open Source Group. --- --- @author ruki --- @file trybuild.lua --- - --- imports -import("core.base.option") -import("core.platform.environment") -import("lib.detect.find_file") - --- try building for makefile -function _build_for_makefile(buildfile) - os.vrun("make -j4") -end - --- try building for configure -function _build_for_configure(buildfile) - if not os.isfile("configure") then - if os.isfile("autogen.sh") then - os.vrunv("sh", {"./autogen.sh"}) - elseif os.isfile("configure.ac") then - os.vrun("autoreconf --install --symlink") - end - end - os.vrun("./configure --prefix=%s", path.absolute("install")) - os.vrun("make -j4") - os.vrun("make install") -end - --- try building for cmakelist -function _build_for_cmakelists(buildfile) - os.mkdir("build") - os.cd("build") - if is_host("windows") and os.arch() == "x64" then - os.vrun("cmake -A x64 -DCMAKE_INSTALL_PREFIX=\"%s\" ..", path.absolute("install")) - else - os.vrun("cmake -DCMAKE_INSTALL_PREFIX=\"%s\" ..", path.absolute("install")) - end - if is_host("windows") then - - local slnfile = assert(find_file("*.sln", os.curdir()), "*.sln file not found!") - os.vrun("msbuild \"%s\" -nologo -t:Rebuild -p:Configuration=Release -p:Platform=%s", slnfile, os.arch() == "x64" and "x64" or "Win32") - - local projfile = os.isfile("INSTALL.vcxproj") and "INSTALL.vcxproj" or "INSTALL.vcproj" - os.vrun("msbuild \"%s\" /property:configuration=Release", projfile) - else - os.vrun("make -j4") - os.vrun("make install") - end -end - --- build for *.sln -function _build_for_sln(buildfile) - os.vrun("msbuild \"%s\" -nologo -t:Rebuild -p:Configuration=Release -p:Platform=%s", buildfile, os.arch() == "x64" and "x64" or "Win32") -end - --- build for *.xcworkspace or *.xcodeproj -function _build_for_xcode(buildfile) - os.vrun("xcodebuild clean build CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO") -end - --- the main entry -function main(targetname) - - -- trace - cprint("${color.warning}xmake.lua not found, try building ..") - - -- enter toolchains environment - environment.enter("toolchains") - - -- TODO *.vcproj, premake.lua, scons, autogen.sh, Makefile.am, ... - -- init build scripts - local buildscripts = {} - if is_host("windows") then - table.insert(buildscripts, {"*.sln", _build_for_sln}) - elseif is_host("macosx") then - table.insert(buildscripts, {"*.xcworkspace", _build_for_xcode}) - table.insert(buildscripts, {"*.xcodeproj", _build_for_xcode}) - end - table.insert(buildscripts, {"CMakeLists.txt", _build_for_cmakelists}) - table.insert(buildscripts, {"configure", _build_for_configure}) - table.insert(buildscripts, {"configure.ac", _build_for_configure}) - table.insert(buildscripts, {"[mM]akefile", _build_for_makefile}) - - -- attempt to build it - local ok = false - for _, buildscript in pairs(buildscripts) do - - -- save the current directory - local oldir = os.curdir() - - -- try building - ok = try - { - function () - - -- attempt to build it if file exists - local files = os.filedirs(buildscript[1]) - if #files > 0 then - - -- trace - print("%s found", path.filename(files[1])) - - -- build it - buildscript[2](files[1]) - return true - end - end, - - catch - { - function (errors) - - -- trace verbose info - if errors then - vprint(errors) - end - end - } - } - - -- restore directory - os.cd(oldir) - - -- ok? - if ok then break end - end - - -- leave toolchains environment - environment.leave("toolchains") - - -- trace - if ok then - cprint("${bright}build ok!${clear}") - else - raise("build failed!") - end -end - - diff --git a/xmake/actions/build/trybuild/autotools.lua b/xmake/actions/build/trybuild/autotools.lua new file mode 100644 index 000000000..1486de46e --- /dev/null +++ b/xmake/actions/build/trybuild/autotools.lua @@ -0,0 +1,45 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file autotools.lua +-- + +-- imports +import("core.base.option") +import("lib.detect.find_file") + +-- detect build-system and configuration file +function detect() + return find_file("configure", os.curdir()) or find_file("configure.ac", os.curdir()) +end + +-- do build +function build() + if not os.isfile("configure") then + if os.isfile("autogen.sh") then + os.execv("sh", {"./autogen.sh"}) + elseif os.isfile("configure.ac") then + os.exec("autoreconf --install --symlink") + end + end + os.mkdir("build/install") + os.exec("./configure --prefix=%s", path.absolute("build/install")) + os.exec("make -j4") + os.exec("make install") +end + + diff --git a/xmake/actions/build/trybuild/cmake.lua b/xmake/actions/build/trybuild/cmake.lua new file mode 100644 index 000000000..2615a64da --- /dev/null +++ b/xmake/actions/build/trybuild/cmake.lua @@ -0,0 +1,52 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file cmake.lua +-- + +-- imports +import("core.base.option") +import("lib.detect.find_file") + +-- detect build-system and configuration file +function detect() + return find_file("CMakeLists.txt", os.curdir()) +end + +-- do build +function build() + os.mkdir("build") + os.cd("build") + if is_host("windows") and os.arch() == "x64" then + os.exec("cmake -A x64 -DCMAKE_INSTALL_PREFIX=\"%s\" ..", path.absolute("build/install")) + else + os.exec("cmake -DCMAKE_INSTALL_PREFIX=\"%s\" ..", path.absolute("build/install")) + end + if is_host("windows") then + + local slnfile = assert(find_file("*.sln", os.curdir()), "*.sln file not found!") + os.exec("msbuild \"%s\" -nologo -t:Rebuild -p:Configuration=Release -p:Platform=%s", slnfile, os.arch() == "x64" and "x64" or "Win32") + + local projfile = os.isfile("INSTALL.vcxproj") and "INSTALL.vcxproj" or "INSTALL.vcproj" + os.exec("msbuild \"%s\" /property:configuration=Release", projfile) + else + os.exec("make -j4") + os.exec("make install") + end +end + + diff --git a/xmake/actions/build/trybuild/make.lua b/xmake/actions/build/trybuild/make.lua new file mode 100644 index 000000000..9889ebb40 --- /dev/null +++ b/xmake/actions/build/trybuild/make.lua @@ -0,0 +1,35 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file make.lua +-- + +-- imports +import("core.base.option") +import("lib.detect.find_file") + +-- detect build-system and configuration file +function detect() + return find_file("[mM]akefile", os.curdir()) +end + +-- do build +function build() + os.exec("make -j4") +end + + diff --git a/xmake/actions/build/trybuild/msbuild.lua b/xmake/actions/build/trybuild/msbuild.lua new file mode 100644 index 000000000..3da3f0bb9 --- /dev/null +++ b/xmake/actions/build/trybuild/msbuild.lua @@ -0,0 +1,37 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file msbuild.lua +-- + +-- imports +import("core.base.option") +import("lib.detect.find_file") + +-- detect build-system and configuration file +function detect() + if is_host("windows") then + return find_file("*.sln", os.curdir()) + end +end + +-- do build +function build() + os.exec("msbuild \"%s\" -nologo -t:Rebuild -p:Configuration=Release -p:Platform=%s", buildfile, os.arch() == "x64" and "x64" or "Win32") +end + + diff --git a/xmake/actions/build/trybuild/xcodebuild.lua b/xmake/actions/build/trybuild/xcodebuild.lua new file mode 100644 index 000000000..55a1bda1c --- /dev/null +++ b/xmake/actions/build/trybuild/xcodebuild.lua @@ -0,0 +1,37 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xcodebuild.lua +-- + +-- imports +import("core.base.option") +import("lib.detect.find_directory") + +-- detect build-system and configuration file +function detect() + if is_host("macosx") then + return find_directory("*.xcworkspace", os.curdir()) or find_directory("*.xcodeproj", os.curdir()) + end +end + +-- do build +function build() + os.exec("xcodebuild clean build CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO") +end + + diff --git a/xmake/actions/build/xmake.lua b/xmake/actions/build/xmake.lua index 51fd34302..2d57e554a 100644 --- a/xmake/actions/build/xmake.lua +++ b/xmake/actions/build/xmake.lua @@ -46,7 +46,9 @@ task("build") , {'j', "jobs", "kv", tostring(math.ceil(os.cpuinfo().ncpu * 3 / 2)), "Specifies the number of jobs to build simultaneously." } , {'w', "warning", "k", false , "Enable the warnings output." } - , {'t', "try", "k", false , "Try building project using third-party buildsystem." } + , {nil, "try", "k", false , "Try building project using third-party buildsystem." } + , {nil, "try-tool", "kv", nil , "Set the third-party buildsystem tool when `--try` is enabled." + , values = {"make", "cmake", "autotools", "msbuild", "xcodebuild"}} , {nil, "files", "kv", nil , "Build the given source files.", "e.g. ", " - xmake --files=src/main.c", |
