Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Harmony SDK support #4874

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ $ xmake f --menu
* MinGW (i386, x86_64, arm, arm64)
* Cygwin (i386, x86_64)
* Wasm (wasm32, wasm64)
* Haiku (i386, x86_64)
* Harmony (x86_64, armeabi-v7a, arm64-v8a)
* Cross (cross-toolchains ..)

## Supported toolchains
Expand Down Expand Up @@ -242,6 +244,7 @@ masm32 The MASM32 SDK
iverilog Icarus Verilog
verilator Verilator open-source SystemVerilog simulator and lint system
cosmocc build-once run-anywhere
hdk Harmony SDK
```

## Supported languages
Expand Down
2 changes: 2 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ $ xmake f --menu
* Cygwin (i386, x86_64)
* Wasm (wasm32, wasm64)
* Haiku (i386, x86_64)
* Harmony (x86_64, armeabi-v7a, arm64-v8a)
* Cross (cross-toolchains ..)

## 支持工具链
Expand Down Expand Up @@ -304,6 +305,7 @@ masm32 The MASM32 SDK
iverilog Icarus Verilog
verilator Verilator open-source SystemVerilog simulator and lint system
cosmocc build-once run-anywhere
hdk Harmony SDK
```

## 支持语言
Expand Down
111 changes: 111 additions & 0 deletions xmake/modules/detect/sdks/find_hdk.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
--!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-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file find_hdk.lua
--

-- imports
import("core.base.option")
import("core.base.global")
import("core.project.config")
import("core.cache.detectcache")
import("lib.detect.find_directory")

-- find hdk directory
function _find_hdkdir(sdkdir)

-- get hdk directory
if not sdkdir then
if not sdkdir then
if is_host("macosx") then
sdkdir = find_directory("native", "~/Library/Huawei/Sdk/*/*")
elseif is_host("windows") then
sdkdir = find_directory("native", "~/Huawei/Sdk/*/*")
end
end
end

-- get hdk directory
if sdkdir and os.isdir(sdkdir) then
return path.translate(sdkdir)
end
end

-- find the hdk toolchain
function _find_hdk(sdkdir)

-- find hdk root directory
sdkdir = _find_hdkdir(sdkdir)
if not sdkdir then
return
end

-- get the binary directory
local bindir = path.join(sdkdir, "llvm", "bin")
if not os.isdir(bindir) then
return
end

local sysroot = path.join(sdkdir, "sysroot")
return {sdkdir = sdkdir,
bindir = bindir,
sysroot = sysroot}
end

-- find hdk toolchains
--
-- @param sdkdir the hdk directory
-- @param opt the argument options
-- e.g. {verbose = true, force = false}
--
-- @return the hdk toolchains. e.g. {bindir = .., cross = ..}
--
-- @code
--
-- local toolchain = find_hdk("/xxx/android-hdk-r10e")
--
-- @endcode
--
function main(sdkdir, opt)
opt = opt or {}

-- attempt to load cache first
local key = "detect.sdks.find_hdk"
local cacheinfo = detectcache:get(key) or {}
if not opt.force and cacheinfo.hdk and cacheinfo.hdk.sdkdir and os.isdir(cacheinfo.hdk.sdkdir) then
return cacheinfo.hdk
end

-- find hdk
local hdk = _find_hdk(sdkdir or config.get("sdk") or global.get("sdk"))
if hdk and hdk.sdkdir then
config.set("hdk", hdk.sdkdir, {force = true, readonly = true})
if opt.verbose or option.get("verbose") then
cprint("checking for Harmony SDK directory ... ${color.success}%s", hdk.sdkdir)
end
else
if opt.verbose or option.get("verbose") then
cprint("checking for Harmony SDK directory ... ${color.nothing}${text.nothing}")
end
end

-- save to cache
cacheinfo.hdk = hdk or false
detectcache:set(key, cacheinfo)
detectcache:save()
return hdk
end
8 changes: 1 addition & 7 deletions xmake/modules/detect/sdks/find_ndk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -275,26 +275,20 @@ function main(sdkdir, opt)
end

-- get arch
local arch = opt.arch or config.get("arch") or "armv7-a"
local arch = opt.arch or config.get("arch") or "armeabi-v7a"

-- find ndk
local ndk = _find_ndk(sdkdir or config.get("ndk") or global.get("ndk"), arch, opt.sdkver or config.get("ndk_sdkver"), opt.toolchains_ver or config.get("ndk_toolchains_ver"))
if ndk and ndk.sdkdir then

-- save to config
config.set("ndk", ndk.sdkdir, {force = true, readonly = true})
config.set("ndkver", ndk.ndkver, {force = true, readonly = true})
config.set("ndk_sdkver", ndk.sdkver, {force = true, readonly = true})
config.set("ndk_toolchains_ver", ndk.toolchains_ver, {force = true, readonly = true})

-- trace
if opt.verbose or option.get("verbose") then
cprint("checking for NDK directory ... ${color.success}%s", ndk.sdkdir)
cprint("checking for SDK version of NDK ... ${color.success}%s", ndk.sdkver)
end
else

-- trace
if opt.verbose or option.get("verbose") then
cprint("checking for NDK directory ... ${color.nothing}${text.nothing}")
end
Expand Down
2 changes: 2 additions & 0 deletions xmake/modules/private/detect/find_platform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ function _find_arch(plat, arch)
else
arch = "x86_64"
end
elseif plat == "harmony" then
arch = "arm64-v8a"
elseif plat == "cross" then
arch = _find_arch_from_cross()
else
Expand Down
35 changes: 35 additions & 0 deletions xmake/platforms/harmony/xmake.lua
Original file line number Diff line number Diff line change
@@ -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-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file xmake.lua
--

platform("harmony")
set_os("harmony")
set_hosts("macosx", "linux", "windows")
set_archs("armeabi-v7a", "arm64-v8a", "x86", "x86_64")

set_formats("static", "lib$(name).a")
set_formats("object", "$(name).o")
set_formats("shared", "lib$(name).so")
set_formats("symbol", "$(name).sym")

set_toolchains("envs", "hdk")




59 changes: 59 additions & 0 deletions xmake/toolchains/hdk/check.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--!A cross-toolchain 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-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file check.lua
--

-- imports
import("core.base.option")
import("core.project.config")
import("detect.sdks.find_hdk")

-- check the hdk toolchain
function _check_hdk(toolchain)
local hdk
for _, package in ipairs(toolchain:packages()) do
local installdir = package:installdir()
if installdir and os.isdir(installdir) then
hdk = find_hdk(installdir, {force = true, verbose = option.get("verbose")})
if hdk then
break
end
end
end
if not hdk then
hdk = find_hdk(toolchain:config("hdk") or config.get("sdk"), {force = true, verbose = true})
end
if hdk then
toolchain:config_set("sdkdir", hdk.sdkdir)
toolchain:config_set("bindir", hdk.bindir)
toolchain:config_set("sysroot", hdk.sysroot)
toolchain:configs_save()
return true
else
--[[TODO we also need to add this tips when use remote hdk toolchain
-- failed
cprint("${bright color.error}please run:")
cprint(" - xmake config --hdk=xxx")
cprint("or - xmake global --hdk=xxx")
raise()]]
end
end

function main(toolchain)
return _check_hdk(toolchain)
end
86 changes: 86 additions & 0 deletions xmake/toolchains/hdk/load.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--!A cross-toolchain 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-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file load.lua
--

-- imports
import("core.base.hashset")
import("core.project.config")

-- get target
function _get_target(arch)
local targets = {
["armeabi-v7a"] = "armv7-linux-ohos"
, ["arm64-v8a"] = "aarch64-linux-ohos"
, ["x86_64"] = "x86_64-linux-ohos"
}
assert(targets[arch], "unknown arch(%s) for harmony!", arch)
return targets[arch]
end

-- load hdk toolchain
function main(toolchain)

-- get gcc toolchain bin directory
local gcc_toolchain_bin = nil
local gcc_toolchain = toolchain:config("gcc_toolchain")
if gcc_toolchain then
gcc_toolchain_bin = path.join(gcc_toolchain, "bin")
end

-- get sdk directory
local sdkdir = toolchain:sdkdir()

-- set toolset
toolchain:set("toolset", "cc", "clang")
toolchain:set("toolset", "cxx", "clang++")
toolchain:set("toolset", "cpp", "clang -E")
toolchain:set("toolset", "as", "clang")
toolchain:set("toolset", "ld", "clang++", "clang")
toolchain:set("toolset", "sh", "clang++", "clang")
toolchain:set("toolset", "ar", "llvm-ar")
toolchain:set("toolset", "ranlib", "llvm-ranlib")
toolchain:set("toolset", "strip", "llvm-strip")

-- add hdk target
local arch = toolchain:arch()
local target = _get_target(arch)
toolchain:add("cxflags", "--target=" .. target)
toolchain:add("asflags", "--target=" .. target)
toolchain:add("ldflags", "--target=" .. target)
toolchain:add("shflags", "--target=" .. target)

-- add sysroot
local sysroot = toolchain:config("sysroot")
if sysroot then
toolchain:add("cxflags", "--sysroot=" .. sysroot)
toolchain:add("asflags", "--sysroot=" .. sysroot)
toolchain:add("ldflags", "--sysroot=" .. sysroot)
toolchain:add("shflags", "--sysroot=" .. sysroot)
end

-- init cxflags for the target kind: binary
toolchain:add("binary.cxflags", "-fPIE", "-pie")

-- add "-fPIE -pie" to ldflags
toolchain:add("ldflags", "-fPIE")
toolchain:add("ldflags", "-pie")

-- add some builtin flags
toolchain:add("cxflags", "-D__MUSL__")
end
29 changes: 29 additions & 0 deletions xmake/toolchains/hdk/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--!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-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file xmake.lua
--

-- harmony sdk toolchain
toolchain("hdk")
set_kind("standalone")
set_homepage("https://developer.harmonyos.com/cn/develop/")
set_description("Harmony SDK")
set_runtimes("c++_static", "c++_shared")

on_check("check")
on_load("load")