惯例先放hello world

hello world
hello world

准备环境

  • 安装 docker
  • docker pull ystyle/open-harmony

ps: 本文使用与鸿蒙系统一同编译的方法。 如果自己有本地环境,可以把lua项目放鸿蒙代码目录里(或者使用软接连)

下载lua官方代码

1
2
3
mkdir -p ~/code/ohos/
cd ~/code/ohos/
git clone https://github.com/lua/lua.git

编写BUILD.gn文件

因为要与系统一起编译, 为了方便,直接用替换掉示例的方法,这样就只需要写一个BUILD.gn就好了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright (c) 2020 YSTYLE(lxy5266@live.com)
# 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.

import("//build/lite/config/component/lite_component.gni")
import("//build/lite/ndk/ndk.gni")

static_library("hello_world") {
sources = [
"lapi.c",
"lauxlib.c",
"lbaselib.c",
"lcode.c",
"lcorolib.c",
"lctype.c",
"ldblib.c",
"ldebug.c",
"ldo.c",
"ldump.c",
"lfunc.c",
"lgc.c",
"linit.c",
"liolib.c",
"llex.c",
"lmathlib.c",
"lmem.c",
"loadlib.c",
"lobject.c",
"lopcodes.c",
"loslib.c",
"lparser.c",
"lstate.c",
"lstring.c",
"lstrlib.c",
"ltable.c",
"ltablib.c",
"ltests.c",
"ltm.c",
"lua.c",
"lundump.c",
"lutf8lib.c",
"lvm.c",
"lzio.c"
]

include_dirs = [
"include",
]
}

lite_component("camera_app") {
target_type = "executable"

features = [
":hello_world",
]
}

ndk_lib("app_sample") {
deps = [
":hello_world"
]
head_files = [
"include"
]
}

static_library里的source参照lua/makefile

编译脚本

创建编译脚本build-ohos.sh文件

1
2
3
cd ~/code/ohos/lua
touch build-ohos.sh
chmod +x build-ohos.sh

文件内容如下

1
2
3
4
5
6
7
8
9
10
11
set -e
rm -rf ./out ./bin
docker run --rm -ti \
-e HARDWARE=ipcamera_hi3516dv300 \
-v ${PWD}/out:/OpenHarmony/out \
-v ${PWD}:/OpenHarmony/applications/sample/camera/app \
ystyle/open-harmony
mkdir -p ./bin
cp ./out/ipcamera_hi3516dv300/bin/camera_app ./bin/lua
tar -zcf lua-5.4.2-ohos.tar.gz ./bin
echo 'build success!'

与鸿蒙一起编译,这里使用我之前的docker镜像

1
2
3
cd ~/code/ohos/lua
./build-ohos.sh
# 看到 ohos ipcamera_hi3516dv300 build success! build success! 就编译成功了。

编译后软件在鸿蒙的 ./out/ipcamera_hi3516dv300/bin/camera_app
脚本会把lua单独打包出来
单独的lua可执行文件在bin目录

演示

io操作
io操作
官方测试用例1
官方测试用例1
官方测试用例2
官方测试用例2
官方测试用例3
官方测试用例3