GameLib is a collection of libraries for creating applications in Cakelisp.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

154 lines
5.9 KiB

;; Oniguruma: a regular expression engine with multiple encodings supported
(add-cakelisp-search-directory "Dependencies/cakelisp/runtime")
(import "CHelpers.cake" "BuildTools.cake" "Dependencies.cake")
(export-and-evaluate
(add-c-search-directory-module "Dependencies/oniguruma/src")
(c-preprocessor-define ONIG_STATIC)
(c-import "oniguruma.h"))
(var-global c-onig-free-self-and-contents (const int) 1)
(comptime-cond
('auto-test
(c-import "<string.h>" "<stdio.h>")
(defun test--oniguruma (&return int)
;; Engine setup
(var encodings ([] OnigEncoding) (array ONIG_ENCODING_UTF8))
(onig_initialize encodings (array-size encodings))
;; Set up regex
(var pattern (* (const char)) "(.)[abc](.)")
(var regex (* regex_t))
(var error-info OnigErrorInfo)
(var regex-compile-result int
(onig_new (addr regex) (type-cast pattern (* (const OnigUChar)))
(type-cast (+ pattern (strlen pattern)) (* (const OnigUChar)))
ONIG_OPTION_DEFAULT ONIG_ENCODING_UTF8 ONIG_SYNTAX_DEFAULT
(addr error-info)))
(unless (= regex-compile-result ONIG_NORMAL)
(var error-message ([] ONIG_MAX_ERROR_MESSAGE_LEN char))
(onig_error_code_to_str (type-cast error-message (* UChar))
regex-compile-result (addr error-info))
(fprintf stderr "error: %s\n" error-message)
(return 1))
(var search-string (* (const char)) "The quick brown fox jumps over the lazy dog")
(var start (* (const char)) search-string)
(var end (* (const char)) (+ search-string (strlen search-string)))
(var range (* (const char)) end) ;; Provided to work with reverse searches
(var region (* OnigRegion) (onig_region_new))
(var result int
(onig_search regex (type-cast search-string (* UChar))
(type-cast end (* UChar))
(type-cast start (* UChar))
(type-cast range (* UChar))
region
ONIG_OPTION_NONE))
(cond
((>= result 0)
(each-in-range (path region > num_regs) i
(fprintf stderr "%d: (%d-%d)\n" i (at i (path region > beg))
(at i (path region > end)))))
((= result ONIG_MISMATCH)
(fprintf stderr "Expected to find results, found none\n")
(onig_region_free region c-onig-free-self-and-contents)
(onig_free regex)
(onig_end)
(return 1))
(true ;; Result < 0
(var error-message ([] ONIG_MAX_ERROR_MESSAGE_LEN char))
(onig_error_code_to_str (type-cast error-message (* UChar))
result)
(fprintf stderr "error: %s\n" error-message)
(onig_region_free region c-onig-free-self-and-contents)
(onig_free regex)
(onig_end)
(return 1)))
;; Clean up
(onig_region_free region c-onig-free-self-and-contents)
(onig_free regex)
(onig_end)
(return 0))))
(defun-comptime build-oniguruma (manager (& ModuleManager) module (* Module) &return bool)
(var oniguruma-archive (* (const char))
(comptime-cond ('Windows "Dependencies/oniguruma/onig_s.lib")
('Unix "cakelisp_cache/OnigurumaInstallDir/lib/libonig.a")))
;; Already built?
;; We could enhance this by checking for modifications, but that's pretty rare
(when (and (fileExists oniguruma-archive))
(return true))
(comptime-cond
('Unix
(Log "Oniguruma: Building via Configure and Make\n")
(var working-dir (* (const char)) "cakelisp_cache/OnigurumaBuildDir")
(makeDirectory working-dir)
(var output-dir (* (const char)) "cakelisp_cache/OnigurumaInstallDir")
(makeDirectory output-dir)
(var configure-output-prefix ([] MAX_PATH_LENGTH char) (array 0))
(scope ;; Output must be absolute directory
(var absolute-output-path (* (const char))
(makeAbsolutePath_Allocated null output-dir))
(unless absolute-output-path
(Logf "error: failed to make Oniguruma output directory '%s'\n" output-dir)
(return false))
(PrintfBuffer configure-output-prefix "--prefix=%s" absolute-output-path)
(free (type-cast absolute-output-path (* void))))
(run-process-sequential-or
("autoreconf" "-vfi" :in-directory "Dependencies/oniguruma")
(Log
"failed at Oniguruma autoreconf step. This requires autoconf to execute.")
(return false))
(run-process-sequential-or
("sh" "../../Dependencies/oniguruma/configure" configure-output-prefix :in-directory working-dir)
(Log
"failed at Oniguruma configure step. This requires a sh/bash-style shell to execute.")
(return false))
(run-process-sequential-or
("make" "--jobs=8" :in-directory working-dir)
(Log "failed at Oniguruma make. This tool requires Makefile support.")
(return false))
(run-process-sequential-or
("make" "install" :in-directory working-dir)
(Log
"failed at Oniguruma make install. Was there a configuration issue with --prefix?")
(return false)))
('Windows
;; TODO: This assumes we're running with VCVars!
(run-process-sequential-or
("C:\\Windows\\System32\\cmd.exe" "/C" "make_win64.bat" :in-directory "Dependencies/oniguruma")
(Log "failed to build Oniguruma via make_win64\n")
(return false))))
;; One final to check to ensure everything's good to go
(unless (fileExists oniguruma-archive)
(Logf
"error: Oniguruma build sequence completed, but files are not where expected. Is there an issue
with the configuration?\nFile expected:\n\t%s\n" oniguruma-archive)
(return false))
(return true))
(add-compile-time-hook-module pre-build build-oniguruma)
(comptime-cond
('Unix
(add-static-link-objects "cakelisp_cache/OnigurumaInstallDir/lib/libonig.a"))
('Windows
(add-static-link-objects "Dependencies/oniguruma/onig_s.lib")))
(add-dependency-git-submodule
clone-oniguruma
"https://github.com/kkos/oniguruma.git"
"Dependencies/oniguruma")