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 raylib support #235

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion android/compile.v
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,41 @@ pub fn compile_v_to_c(opt CompileOptions) !VMetaInfo {
return v_meta_dump
}

fn build_raylib(opt CompileOptions, raylib_path string, arch string) ! {
build_path := os.join_path(raylib_path, 'build')
// check if the library already exists or compile it
if os.exists(os.join_path(build_path, arch, 'libraylib.a')) {
return
} else {
src_path := os.join_path(raylib_path, 'src')
ndk_path := ndk.root()
os.execute('make -C ${src_path} clean')
arch_name := if arch == 'arm64-v8a' {
Copy link
Contributor

@MatejMagat305 MatejMagat305 Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be more eficient and nicer with mach{...} instead of if ... else if ... else?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would look cleaner with match. The else branch for the match would take care of the following if as well.

'arm64'
} else if arch == 'armeabi-v7a' {
'arm'
} else if arch in ['x86', 'x86_64'] {
arch
} else {
''
}
if arch_name !in ['arm64', 'arm', 'x86', 'x86_64'] {
return error('${arch_name} is now a known architecture')
}
os.execute('make -C ${src_path} PLATFORM=PLATFORM_ANDROID ANDROID_NDK=${ndk_path} ANDROID_ARCH=${arch_name} ANDROID_API_VERSION=${opt.api_level} ')
taget_path := os.join_path(build_path, arch)
os.mkdir_all(taget_path) or { return error('failed making directory "${taget_path}"') }
os.mv(os.join_path(src_path, 'libraylib.a'), taget_path) or {
return error('failed to move .a file from ${src_path} to ${taget_path}')
}
}
}

fn download_raylib(raylib_path string) {
// clone raylib from github
os.execute('git clone https://github.com/raysan5/raylib.git ${raylib_path}')
}

pub fn compile(opt CompileOptions) ! {
err_sig := @MOD + '.' + @FN
os.mkdir_all(opt.work_dir) or {
Expand All @@ -190,6 +225,30 @@ pub fn compile(opt CompileOptions) ! {
err: err.msg()
})
}

is_raylib := 'mohamedlt.vraylib' in v_meta_dump.imports

// check if raylib floder is found else clone it
if is_raylib {
raylib_path := os.join_path(vxt.vmodules() or {
return error('${err_sig}:vmodules folder not found')
}, 'vab', 'raylib')
if os.exists(raylib_path) {
for arch in opt.archs {
build_raylib(opt, raylib_path, arch) or {
return error('cant build raylib ERROR: ${err}')
}
}
} else {
download_raylib(raylib_path)
for arch in opt.archs {
build_raylib(opt, raylib_path, arch) or {
return error('cant build raylib ERROR: ${err}')
}
}
}
}

v_cflags := v_meta_dump.c_flags
imported_modules := v_meta_dump.imports

Expand Down Expand Up @@ -325,6 +384,14 @@ pub fn compile(opt CompileOptions) ! {

is_debug_build := opt.is_debug_build()

// add needed flags for raylib
if is_raylib {
ldflags << '-lEGL'
ldflags << '-lGLESv2'
ldflags << '-u ANativeActivity_onCreate'
ldflags << '-lOpenSLES'
}

// Sokol sapp
if 'sokol.sapp' in imported_modules {
if opt.verbosity > 1 {
Expand Down Expand Up @@ -453,14 +520,22 @@ pub fn compile(opt CompileOptions) ! {
arch_o_files := o_files[arch].map('"${it}"')
arch_a_files := a_files[arch].map('"${it}"')

build_cmd := [
mut build_cmd := [
arch_cc[arch],
arch_o_files.join(' '),
'-o "${arch_lib_dir}/lib${opt.lib_name}.so"',
arch_a_files.join(' '),
'-L"' + arch_libs[arch] + '"',
ldflags.join(' '),
]
lflags := os.join_path(vxt.vmodules() or {
return error('${err_sig}:vmodules folder not found')
}, 'vab', 'raylib', 'build', arch)

// add the compiled raylib libraries for each arch
if is_raylib {
build_cmd << '-L ${lflags}'
Copy link
Contributor

@MatejMagat305 MatejMagat305 Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe initialisation of 'lflags' for raylib would be nice put inside if

}

jobs << job_util.ShellJob{
cmd: build_cmd
Expand Down