Skip to content

Commit

Permalink
klib: add package (#5560)
Browse files Browse the repository at this point in the history
* klib: add package

* improve non-x86 arch build
  • Loading branch information
star-hengxing authored Oct 23, 2024
1 parent 60e50e9 commit 3468d1d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/k/klib/port/rand48.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdlib.h>

double drand48(void) {
return rand() / (RAND_MAX + 1.0);
}

void srand48(long int seedval) {
srand(seedval);
}
8 changes: 8 additions & 0 deletions packages/k/klib/port/unistd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#ifdef _MSC_VER
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#else
typedef int ssize_t;
#endif
52 changes: 52 additions & 0 deletions packages/k/klib/port/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
option("url", {default = false})
option("thread", {default = false})
option("open", {default = false})

add_rules("mode.release", "mode.debug")

add_requires("zlib")
if has_config("url") then
add_requires("libcurl")
add_packages("libcurl")
end

if has_config("thread") then
add_syslinks("pthread")
end

target("klib")
set_kind("$(kind)")
add_files("kmath.c", {defines = "M_SQRT2=1.41421356237309504880"})
add_files("*.c|rand48.c|kurl.c|kthread.c|kopen.c|ksw.c")
add_includedirs(os.projectdir())
add_packages("zlib")

if is_plat("windows") and is_kind("shared") then
add_rules("utils.symbols.export_all")
end

if is_plat("windows", "mingw") then
add_syslinks("ws2_32")
end

add_headerfiles("*.h|unistd.h|kurl.h|kthread.h|kopen.h|ksw.h")

if has_config("url") then
add_files("kurl.c")
add_headerfiles("kurl.h")
end

if has_config("thread") then
add_files("kthread.c")
add_headerfiles("kthread.h")
end

if has_config("open") then
add_files("kopen.c")
add_headerfiles("kopen.h")
end

if is_arch("x64", "x86", "x86_64") then
add_files("ksw.c")
add_headerfiles("ksw.h")
end
53 changes: 53 additions & 0 deletions packages/k/klib/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package("klib")
set_homepage("http://attractivechaos.github.io/klib/")
set_description("A standalone and lightweight C library")
set_license("MIT")

add_urls("https://github.com/attractivechaos/klib.git")
add_versions("2024.06.03", "29445495262cf34f4c3b82d3917ac83f3e1f3f58")

add_configs("url", {description = "Enable FILE-like interfaces to libcurl.", default = false, type = "boolean"})
add_configs("thread", {description = "Enable simple multi-threading models.", default = false, type = "boolean"})
add_configs("open", {description = "Enable smart stream opening.", default = false, type = "boolean"})

if is_plat("windows", "mingw") then
add_syslinks("ws2_32")
end

add_deps("zlib")

on_load(function (package)
if package:config("url") then
package:add("deps", "libcurl")
end
if package:config("thread") then
package:add("syslinks", "pthread")
end
end)

on_install(function (package)
io.replace("bgzf.c", "fp->uncompressed_block + input_length", "(char*)fp->uncompressed_block + input_length", {plain = true})
io.replace("bgzf.c", "fp->compressed_block + 18", "(char*)fp->compressed_block + 18", {plain = true})
io.replace("knetfile.c", "buf + l", "(char*)buf + l", {plain = true})
if package:is_plat("windows", "mingw") then
io.replace("khmm.c", "// new/delete hmm_par_t", [[#include "rand48.c"]], {plain = true})
end

local configs = {
url = package:config("url"),
thread = package:config("thread"),
open = package:config("open"),
}
if package:is_plat("windows", "mingw") then
os.cp(path.join(package:scriptdir(), "port", "rand48.c"), "rand48.c")
if package:is_plat("windows") then
os.cp(path.join(package:scriptdir(), "port", "unistd.h"), "unistd.h")
end
end
os.cp(path.join(package:scriptdir(), "port", "xmake.lua"), "xmake.lua")
import("package.tools.xmake").install(package, configs)
end)

on_test(function (package)
assert(package:has_cfuncs("kmalloc", {includes = "kalloc.h"}))
end)

0 comments on commit 3468d1d

Please sign in to comment.