Compile tolua——2. Basic compilation tolua

Contents
1. Compilation tools and environment instructions
2. Basic compilation of tolua
3. Taking pbc as an example, add third-party libraries
4. Update luaJit

Hello everyone, I am Zhao.
Continue to talk about the compilation of each common platform of tolua.
The official tolua_runtime-master project is used here to compile.
The specific compilation software and source code address required have been introduced in the previous article. First prepare the environment and then start compiling.

1. Compile the dll for Windows

First, take the dll of the Windows platform, which is relatively easy to compile successfully, as a practice hand.

1. Compile 32-bit dll

insert image description here

After executing mingw32_shell.bat
, the command line prompt appears

1. First jump to the path where the project is located

My path is the tolua folder on the G drive, so I use the cd command to jump like this:

admin@DESKTOP-DL702DH MINGW32 ~
$ cd /g/

admin@DESKTOP-DL702DH MINGW32 /g
$ cd tolua

admin@DESKTOP-DL702DH MINGW32 /g/tolua

2. You can use the dir command to display all files in the current folder

$ dir
android         build_ubuntu.sh  iOS             luasocket  struct.c
bit.c           build_win32.sh   LICENSE         macjit     tolua.c
build_arm.sh    build_win64.sh   link_arm64.bat  macnojit   tolua.h
build_arm64.sh  build_x86.sh     lpeg.c          pb.c       ubuntu
build_ios.sh    cjson            lpeg.h          Plugins    uint64.c
build_osx.sh    int64.c          luajit-2.1      README.md  window

3. Call build_win32.sh to compile

admin@DESKTOP-DL702DH MINGW32 /g/tolua
$ ./build_win32.sh

At this time, you will see a lot of output

E:/Software/msys64/mingw64/bin/mingw32-make -C src clean
mingw32-make[1]: Entering directory ‘G:/tolua/luajit-2.1/src’ rm -f
luajit.exe libluajit.a lua51.dll host/minilua.exe host/buildvm.exe
lj_vm.S lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h lj_folddef.h
host/buildvm_arch.h jit/vmdef.lua .o host/.o *.obj *.lib *.exp *.dll
*.exe *.manifest *.pdb *.ilk mingw32-make[1]: Leaving directory ‘G:/tolua/luajit-2.1/src’
==== Building LuaJIT 2.1.0-beta3 ==== E:/Software/msys64/mingw64/bin/mingw32-make -C src mingw32-make[1]:
Entering directory ‘G:/tolua/luajit-2.1/src’ HOSTCC host/minilua.o
HOSTLINK host/minilua.exe DYNASM host/buildvm_arch.h HOSTCC
host/buildvm.o HOSTCC host/buildvm_asm.o HOSTCC
host/buildvm_peobj.o HOSTCC host/buildvm_lib.o HOSTCC
host/buildvm_fold.o HOSTLINK host/buildvm.exe BUILDVM lj_vm.o CC
lj_gc.o BUILDVM lj_ffdef.h CC lj_err.o CC lj_char.o
BUILDVM lj_bcdef.h CC lj_bc.o CC lj_obj.o CC
lj_buf.o CC lj_str.o CC lj_tab.o CC lj_func.o CC
lj_udata.o CC lj_meta.o CC lj_debug.o CC
lj_state.o CC lj_dispatch.o CC lj_vmevent.o CC
lj_vmmath.o CC lj_strscan.o CC lj_strfmt.o CC
lj_strfmt_num.o CC lj_api.o CC lj_profile.o CC
lj_lex.o CC lj_parse.o CC lj_bcread.o CC
lj_bcwrite.o CC lj_load.o CC lj_ir.o CC
lj_opt_mem.o BUILDVM lj_folddef.h CC lj_opt_fold.o CC
lj_opt_narrow.o CC lj_opt_dce.o CC lj_opt_loop.o CC
lj_opt_split.o CC lj_opt_sink.o CC lj_mcode.o CC
lj_snap.o CC lj_record.o CC lj_crecord.o BUILDVM
lj_recdef.h CC lj_ffrecord.o CC lj_asm.o CC
lj_trace.o CC lj_gdbjit.o CC lj_ctype.o CC
lj_cdata.o CC lj_cconv.o CC lj_ccall.o CC
lj_ccallback.o CC lj_carith.o CC lj_clib.o CC
lj_cparse.o CC lj_lib.o CC lj_alloc.o lj_alloc.c:176:14:
warning: ‘DIRECT_MMAP’ defined but not used [-Wunused-function]
static void *DIRECT_MMAP(size_t size)
^ CC lib_aux.o BUILDVM lj_libdef.h CC lib_base.o CC lib_math.o CC lib_bit.o CC
lib_string.o CC lib_table.o CC lib_io.o CC
lib_os.o CC lib_package.o CC lib_debug.o CC
lib_jit.o CC lib_ffi.o CC lib_init.o AR
libluajit.a CC luajit.o BUILDVM jit/vmdef.lua LINK
luajit.exe OK Successfully built LuaJIT mingw32-make[1]:
Leaving directory ‘G:/tolua/luajit-2.1/src’
==== Successfully built LuaJIT 2.1.0-beta3 ==== E:/Software/msys64/mingw64/bin/mingw32-make -C src clean
mingw32-make[1]: Entering directory ‘G:/tolua/luajit-2.1/src’ rm -f
luajit.exe libluajit.a lua51.dll host/minilua.exe host/buildvm.exe
lj_vm.S lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h lj_folddef.h
host/buildvm_arch.h jit/vmdef.lua .o host/.o *.obj *.lib *.exp *.dll
*.exe *.manifest *.pdb *.ilk mingw32-make[1]: Leaving directory ‘G:/tolua/luajit-2.1/src’

These outputs are actually the printouts of each step in the batch process. If no error message is found, the compilation should be successful in the end.

4. Compile successfully

You can find the compiled dll file in the Plugins\x86 folder.

insert image description here

2. Compile 64-bit dll

insert image description here

Note that because it is compiled for 64-bit,
after executing mingw64_shell.bat and exiting the command line, the steps are the same as the above 32-bit, jump to the directory where tolua is located, and execute build_win64.sh this time.
The specific process is similar to that of 32-bit, so I will not post the process anymore.
After the compilation is successful, find the generated dll file in the Plugins/x86_64 folder
insert image description here

Here is a little look at what build_64.sh does, open the file with a text editor

#!/bin/bash
# 64 Bit Version
mkdir -p window/x86_64

cd luajit-2.1
mingw32-make clean

mingw32-make BUILDMODE=static CC="gcc -m64 -O2" XCFLAGS=-DLUAJIT_ENABLE_GC64
cp src/libluajit.a ../window/x86_64/libluajit.a
mingw32-make clean

cd ..

gcc -m64 -O2 -std=gnu99 -shared \
 tolua.c \
 int64.c \
 uint64.c \
 pb.c \
 lpeg.c \
 struct.c \
 cjson/strbuf.c \
 cjson/lua_cjson.c \
 cjson/fpconv.c \
 luasocket/auxiliar.c \
 luasocket/buffer.c \
 luasocket/except.c \
 luasocket/inet.c \
 luasocket/io.c \
 luasocket/luasocket.c \
 luasocket/mime.c \
 luasocket/options.c \
 luasocket/select.c \
 luasocket/tcp.c \
 luasocket/timeout.c \
 luasocket/udp.c \
 luasocket/wsocket.c \
 -o Plugins/x86_64/tolua.dll \
 -I./ \
 -Iluajit-2.1/src \
 -Iluasocket \
 -lws2_32 \
 -Wl,--whole-archive window/x86_64/libluajit.a -Wl,--no-whole-archive -static-libgcc -static-libstdc++

As you can see from this batch script, it actually does the following things:

1. Create the window/x86_64 folder

mkdir -p window/x86_64

2. Enter the luajit-2.1 folder, and use make to compile libluajit.a

cd luajit-2.1
mingw32-make clean

mingw32-make BUILDMODE=static CC="gcc -m64 -O2" XCFLAGS=-DLUAJIT_ENABLE_GC64
cp src/libluajit.a ../window/x86_64/libluajit.a
mingw32-make clean

3. Return to the previous folder,

cd ..

4. Compile with gcc, specify the content to be compiled and the folder to be automatically searched

gcc -m64 -O2 -std=gnu99 -shared \
 tolua.c \
 int64.c \
 uint64.c \
 pb.c \
 lpeg.c \
 struct.c \
 cjson/strbuf.c \
 cjson/lua_cjson.c \
 cjson/fpconv.c \
 luasocket/auxiliar.c \
 luasocket/buffer.c \
 luasocket/except.c \
 luasocket/inet.c \
 luasocket/io.c \
 luasocket/luasocket.c \
 luasocket/mime.c \
 luasocket/options.c \
 luasocket/select.c \
 luasocket/tcp.c \
 luasocket/timeout.c \
 luasocket/udp.c \
 luasocket/wsocket.c \
 -o Plugins/x86_64/tolua.dll \
 -I./ \
 -Iluajit-2.1/src \
 -Iluasocket \
 -lws2_32 \
 -Wl,--whole-archive window/x86_64/libluajit.a -Wl,--no-whole-archive -static-libgcc -static-libstdc++

Generally speaking, compiling the Windows dll is the easiest, and there will be no errors in the middle, so you can try this first.

2. Compile the so for Android

1. Compile 32-bit so

Execute mingw32_shell.bat, and jump to the directory where tolua is located
This time, after executing build_arm.sh
, I found a bunch of error reports, the main ones are like this:

D:/android-ndk-r10e/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64/bin/aarch64-linux-android-gcc: command not found
insert image description here

This is because, in build_arm.sh, you need to specify the NDK directory first, and the default is D:/android-ndk-r10e. My computer does not have this NDK directory locally, so it must not be found.

Next, you need to download the NDK. This download can be done by Baidu. However, it is recommended to read what I said below before downloading, otherwise it may waste a lot of time.
For example, I downloaded the newer NDK version r25c, then filled in the address into build_arm.sh, and then executed the compilation. This time a new error occurred:

G:/android-ndk-r25c/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64/bin/aarch64-linux-android-gcc: command not found

This is because the later versions of Android have completely removed gcc compilation, and changed to use clang compilation by default. After ndk18, there is no gcc. There are only two solutions to this problem: 1.
Use
the old version that includes gcc ndk
2.
The simple way to learn to compile with clang is to use the old version of ndk. However, it should be noted that even if it is an old version of ndk, the gcc method it contains will be different depending on the version. For example, I will compile with r16 next week. , will report an error:

g:\ndk\android-ndk-r16b-windows-x86_64\android-ndk-r16b\toolchains\aarch64-linux-android-4.9\prebuilt\windows-x86_64\lib\gcc\aarch64-linux-android\4.9.x\include-fixed\limits.h:34,
from luaconf.h:12,
from lua.h:16,
from lj_obj.h:12,
from lj_gc.c:12:

Therefore, if you want to compile with the old version of ndk, it is best to use the ndk-r10e version

Downloaded ndk-r10e, specified the path, compiled, and passed this time. You can see the tolua.so file in Plugins\Android\libs\armeabi-v7a
insert image description here

2. Compile 64-bit so

Execute mingw64_shell.bat, jump to the tolua directory, fill in the NDK path into build_arm64.sh, and then execute build_arm64.sh
to find that the execution is complete, android/jni/libluajit.a has been regenerated, but it is not in Plugins\Android \libs\arm64-v8a directory found a newly generated tolua.so file
Open build_arm64.sh to see

cd luajit-2.1/src

# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.0+ (ICS)
NDK=G:/ndk/android-ndk-r13b-windows-x86_64/android-ndk-r13b
NDKABI=21
NDKTRIPLE=aarch64-linux-android
NDKVER=$NDK/toolchains/aarch64-linux-android-4.9
NDKP=$NDKVER/prebuilt/windows-x86_64/bin/aarch64-linux-android-
NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm64" 
NDKARCH="-DLJ_ABI_SOFTFP=0 -DLJ_ARCH_HASFPU=1 -DLUAJIT_ENABLE_GC64=1"

make clean
make HOST_CC="gcc -m64" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF $NDKARCH"
cp ./libluajit.a ../../android/jni/libluajit.a
make clean

if [[ "$OSTYPE" == "msys" ]]; then
	cd ../../
	# can't pass $NDK to bat
	cmd /c "link_arm64.bat"
else
	cd ../../android
	$NDK/ndk-build clean APP_ABI="armeabi-v7a,x86,arm64-v8a" APP_PLATFORM=android-21
	$NDK/ndk-build APP_ABI="arm64-v8a" APP_PLATFORM=android-21
	cp libs/arm64-v8a/libtolua.so ../Plugins/Android/libs/arm64-v8a
	$NDK/ndk-build clean APP_ABI="armeabi-v7a,x86,arm64-v8a" APP_PLATFORM=android-21
fi

can be seen

if [[ "$OSTYPE" == "msys" ]]; then
	cd ../../
	# can't pass $NDK to bat
cmd /c "link_arm64.bat"

We executed it with msys, so he actually called the link_arm64.bat batch.
Open link_arm64.bat to see

@echo off
set ndkPath=D:/android-ndk-r15c
cd ./android
call %ndkPath%/ndk-build clean APP_ABI="armeabi-v7a,x86,arm64-v8a" APP_PLATFORM=android-21
call %ndkPath%/ndk-build APP_ABI="arm64-v8a" APP_PLATFORM=android-21
copy libs\arm64-v8a\libtolua.so ..\Plugins\Android\libs\arm64-v8a
call %ndkPath%/ndk-build clean APP_ABI="armeabi-v7a,x86,arm64-v8a" APP_PLATFORM=android-21
echo Successfully linked
exit

It is found that there is also a path that needs to specify the NDK, so specify your own NDK path.
Compile again and find that it is successful. A newly generated tolua.so file is found in the Plugins\Android\libs\arm64-v8a directory.

Let's look back at the content of build_arm64.sh. What he does in it are:
1. Specify compilation parameters

# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.0+ (ICS)
NDK=D:/android-ndk-r15c
NDKABI=21
NDKTRIPLE=aarch64-linux-android
NDKVER=$NDK/toolchains/aarch64-linux-android-4.9
NDKP=$NDKVER/prebuilt/windows-x86_64/bin/aarch64-linux-android-
NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm64" 
NDKARCH="-DLJ_ABI_SOFTFP=0 -DLJ_ARCH_HASFPU=1 -DLUAJIT_ENABLE_GC64=1"

2. Compile libluajit.a

make clean
make HOST_CC="gcc -m64" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF $NDKARCH"
cp ./libluajit.a ../../android/jni/libluajit.a
make clean

3. Actual compilation

if [[ "$OSTYPE" == "msys" ]]; then
	cd ../../
	# can't pass $NDK to bat
	cmd /c "link_arm64.bat"
else
	cd ../../android
	$NDK/ndk-build clean APP_ABI="armeabi-v7a,x86,arm64-v8a" APP_PLATFORM=android-21
	$NDK/ndk-build APP_ABI="arm64-v8a" APP_PLATFORM=android-21
	cp libs/arm64-v8a/libtolua.so ../Plugins/Android/libs/arm64-v8a
	$NDK/ndk-build clean APP_ABI="armeabi-v7a,x86,arm64-v8a" APP_PLATFORM=android-21
fi

The final actual compilation is actually to call the ndk-build method of NDK to compile the so file

3. Compile the bundle for OSX

Neither compiling osx nor ios can be compiled on Windows, because it requires the command line to call xcode to compile.
So prepare xcode on the mac, then copy the tolua project to the mac, and try to compile. You
don’t need to install any software under the mac, just open the command line terminal, and then enter the command.
You can use the cd command to jump to the folder, and use ls The command lists the contents of the folder, and then calls ./build_osx.sh
to compile directly, and found 2 errors

1. Prompt that i386 does not support

insert image description here

Open the tolua.xcodeproj project under the macnojit folder, search for ARCHS in Build Settings and modify the content in the red box to delete i386

2. Found that cjson reported an error

Go to download the latest version of the cjson
download address
and copy it to the cjson folder in the tolua project.

Then compile again, and you will see tolua.bundle in the Plugins folder

4. Compile the a file for iOS

Same as compiling OSX, use the command line terminal to jump to the tolua project path, and then execute build_ios.sh
to find 2 problems

1. Prompt IPHONEOS_DEPLOYMENT_TARGET error

warning: The iOS deployment target ‘IPHONEOS_DEPLOYMENT_TARGET’ is set
to
8.0, but the range of supported deployment target versions is 9.0 to
14.1.99. (in target ‘FMDB’ from project ‘Pods’)

Follow the prompts to open the tolua.xcodeproj project under the iOS folder of the tolua project
insert image description here

Then find the iOS Deployment Target option of the deployment in Build Settings, and select a version 9.0 to 14.1.99 supported in the prompt, for example, select 9.0

2. Prompt Armv7 error

Search for "Excluded Architectures" in Build Settings, copy and add the following content:

EXCLUDED_ARCHS__EFFECTIVE_PLATFORM_SUFFIX_simulator__NATIVE_ARCH_64_BIT_x86_64=arm64 arm64e armv7 armv7s armv6 armv8EXCLUDED_ARCHS=$(inherited)$(EXCLUDED_ARCHS__EFFECTIVE_PLATFORM_SUFFIX_$(EFFECTIVE_PLATFORM_SUFFIX)__NATIVE_ARCH_64_BIT_$(NATIVE_ARCH_64_BIT))

warning: The iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to
    8.0, but the range of supported deployment target versions is 9.0 to

insert image description here

Execute the compilation again, and you will see libtolua.a in the Plugins\iOS folder

Because the compilation of OSX and iOS is based on xcode, in fact, if an error is reported, it needs to be modified in the corresponding xcode project.

Guess you like

Origin blog.csdn.net/liweizhao/article/details/131138468
Recommended