Skip to content

Commit c99ea28

Browse files
committed
Brief: Update to use janet-native-tools
Summary: Simplify native component build using janet-native-tools to run cmake and to update widget.meta.janet link args.
1 parent ae85889 commit c99ea28

File tree

4 files changed

+70
-117
lines changed

4 files changed

+70
-117
lines changed

bundle/info.jdn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@{:author "Bob Tolbert "
2-
:dependencies @["spork"]
32
:description "Janet wrapper for FLTK"
43
:license "MIT"
5-
:jpm-dependencies @["spork"]
6-
:name "fltk-janet"
4+
:jpm-dependencies @["spork"
5+
{:url "https://github.com/rwtolbert/janet-native-tools.git" :tag "0.2.0"}]
6+
:name "jfltk"
77
:version "0.3.0"}

bundle/init.janet

Lines changed: 67 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
1-
(if (dyn :install-time-syspath)
2-
(use @install-time-syspath/spork/declare-cc)
3-
(use spork/declare-cc))
1+
(use spork/declare-cc)
42

5-
(setdyn :verbose true)
6-
(def- build-type "release")
3+
# force default to release unless specifically requested
4+
(when (not (os/getenv "JANET_BUILD_TYPE"))
5+
(setdyn :build-type :release))
6+
7+
# pull info.jdn so we don't duplicate symbols there and
8+
# in declare-project below
9+
(def info (-> (slurp "./bundle/info.jdn") parse))
10+
11+
(declare-project
12+
:name (info :name)
13+
:description (info :description)
14+
:version (info :version)
15+
:dependencies (info :jpm-dependencies))
16+
17+
###########
18+
(try
19+
(import janet-native-tools :as jnt)
20+
([err fib]
21+
(print "please run `janet-pm deps` or `jeep prep` first")))
722

823
(import spork/pm)
924
(import spork/sh)
1025
(import spork/path)
1126
(use ./utils)
1227

13-
(defdyn *cmakepath* "What cmake command to use")
14-
(defdyn *ninjapath* "What ninja command to use")
15-
16-
(defn- cmake
17-
"Make a call to cmake."
18-
[& args]
19-
(printf "cmake %j" args)
20-
(sh/exec (dyn *cmakepath* "cmake") ;args))
28+
# make sure we can find cmake and make
29+
(jnt/require-git)
30+
(jnt/require-cmake)
31+
(jnt/require-ninja)
2132

2233
(defn- update-submodules []
23-
(pm/git "submodule" "update" "--init" "--recursive"))
24-
25-
(defn- lib-prefix []
26-
(if (= (os/which) :windows)
27-
""
28-
"lib"))
29-
30-
(defn- lib-suffix []
31-
(if (= (os/which) :windows)
32-
".lib"
33-
".a"))
34+
(jnt/git "submodule" "update" "--init" "--recursive"))
3435

3536
(def- cfltk-lib
36-
(string (lib-prefix) "cfltk2" (lib-suffix)))
37+
(string (jnt/gen-static-libname "cfltk2")))
3738

3839
(def- fltk-build-dir (string/format "_build/cfltk-build/fltk/lib"))
3940
(def- fltk-libs
4041
(do
4142
(var results @[])
4243
(loop [item :in ["fltk" "fltk_gl" "fltk_forms" "fltk_images" "fltk_png" "fltk_jpeg" "fltk_z"]]
43-
(array/push results (string (lib-prefix) item (lib-suffix))))
44+
(array/push results (string (jnt/gen-static-libname item))))
4445
results))
4546

4647
(def- cfltk-build-dir (string/format "_build/cfltk-build"))
@@ -57,35 +58,36 @@
5758
(array/push fltk-flags "-DFLTK_BACKEND_WAYLAND=ON"))
5859

5960
(def- cmake-flags (array/concat cfltk-flags fltk-flags))
60-
(def- cmake-build-flags @["--build" cfltk-build-dir "--parallel" "--config" "Release"])
61+
62+
(def [build-cfltk clean-cfltk]
63+
(jnt/declare-cmake :name "cfltk"
64+
:source-dir "cfltk"
65+
:build-dir cfltk-build-dir
66+
:cmake-flags cmake-flags))
6167

6268
(defn- copy-static-libs []
6369
(sh/copy (string/format "%s/%s" cfltk-build-dir cfltk-lib) (string/format "./jfltk/%s" cfltk-lib))
6470
(loop [fname :in fltk-libs]
6571
(let [fullname (string/format "%s/%s" fltk-build-dir fname)
6672
outname (string/format "./jfltk/%s" fname)]
6773
(when (sh/exists? fullname)
68-
(sh/copy fullname outname )))))
74+
(sh/copy fullname outname)))))
6975

7076
(defn- clean-static-libs []
7177
(loop [fname :in (sh/list-all-files "jfltk")]
72-
(when (string/has-suffix? (lib-suffix) fname)
73-
(sh/rm fname))))
74-
75-
(defn build-cfltk []
76-
(unless (and (sh/exists? "cfltk") (sh/exists? "cfltk/fltk"))
77-
(update-submodules))
78-
# remove old static libs, might be stale
79-
(clean-static-libs)
80-
(unless (sh/exists? (string/format "%s/%s" cfltk-build-dir cfltk-lib))
81-
(unless (sh/exists? (string/format "%s/%s" cfltk-build-dir "build.ninja"))
82-
(cmake ;cmake-flags))
83-
(do (cmake ;cmake-build-flags)))
84-
# copy static libs, assuming they have been built
85-
(copy-static-libs))
86-
87-
(set-command "cmake" *cmakepath*)
88-
(set-command "ninja" *ninjapath*)
78+
(each libname fltk-libs
79+
(when (string/has-suffix? libname fname)
80+
(sh/rm fname)))))
81+
82+
(task "build-cfltk" []
83+
(update-submodules)
84+
(clean-static-libs)
85+
(build-cfltk)
86+
(copy-static-libs))
87+
88+
(task "pre-build" ["build-cfltk"])
89+
90+
(task "clean-cfltk" [] (clean-cfltk))
8991

9092
(var cfltk-lib-path nil)
9193
(var fltk-lib-path nil)
@@ -105,47 +107,39 @@
105107
"glu32.lib" "opengl32.lib" "ole32.lib" "uuid.lib" "comctl32.lib" "gdi32.lib" "gdiplus.lib"
106108
"user32.lib" "shell32.lib" "comdlg32.lib" "ws2_32.lib" "winspool.lib"])
107109

108-
(defn fltk-link-libs []
110+
(defn- fltk-link-libs []
109111
(if (sh/exists? fltk-config)
110112
(if (not (= (os/which) :windows))
111113
(do
112114
(def out (sh/exec-slurp fltk-config "--use-gl" "--use-images" "--use-glut" "--use-forms" "--use-cairo" "--ldflags"))
113115
(string/split " " out))
114116
@[])
115117
(do (build-cfltk)
116-
(fltk-link-libs))))
118+
(fltk-link-libs))))
117119

118-
(defn gen-lflags []
120+
(defn- gen-lflags []
119121
(if (= (os/which) :windows)
120122
windows-fltk-link-libs
121123
(array/join @[cfltk-lib-path "-lcfltk2"] (fltk-link-libs))))
122124

123-
(defdyn *lflags* "Linker flags")
124-
(setdyn *lflags* (gen-lflags))
125-
126-
(task "pre-build" ["build-cfltk" "create-flags"])
125+
(var cppflags nil)
126+
(def- lflags (gen-lflags))
127+
(case (os/which)
128+
:windows (set cppflags @["/bigobj" "-I./cfltk/include" "-DCFLTK_USE_GL" "-DFLTK_BUILD_FORMS"])
129+
:macos (set cppflags @["-I./cfltk/include" "-DCFLTK_USE_GL" "-DFLTK_BUILD_FORMS"])
130+
:linux (set cppflags @["-fPIC" "-I./cfltk/include" "-DCFLTK_USE_GL" "-DFLTK_BUILD_FORMS"]))
127131

128-
(dofile "project.janet" :env (jpm-shim-env))
132+
(declare-source
133+
:source ["jfltk"])
129134

130-
(task "build-cfltk" []
131-
(build-cfltk))
132-
133-
# this creates a file in jfltk that can be used to get to the
134-
# platform specific linker flags to compile Janet+FLTK apps into
135-
# full executables. Have a look in "examples/" for an example
136-
(task "create-flags" []
137-
(def flags (gen-lflags))
138-
(var real-flags @[])
139-
(loop [item :in flags]
140-
(if (and (string/has-prefix? "-L" item) (not (string/find "/usr" item)))
141-
(array/push real-flags '(get-libdir))
142-
(array/push real-flags item)))
143-
(def fname (string (os/cwd) "/jfltk/flags.janet"))
144-
(def ofs (file/open fname :w))
145-
(file/write ofs "(import spork/path)\n")
146-
(file/write ofs "(defn get-libdir [] (string \"-L\" (path/abspath (path/dirname (dyn *current-file*)))))\n")
147-
(file/write ofs (string/format "(def lflags %j)\n" real-flags))
148-
(file/write ofs "(defn print-lflags [] (pp lflags))\n")
149-
(file/close ofs))
135+
(declare-native
136+
:name "jfltk/widgets"
137+
:source @["c/module.cpp"]
138+
:c++flags cppflags
139+
:lflags lflags)
150140

141+
# create a new task to run the ldflags fixup
142+
(task "fix-up-ldflags" [] (jnt/fix-up-ldflags "jfltk" "widgets.meta.janet"))
151143

144+
# attach this task to the post-install hook
145+
(task "post-install" ["fix-up-ldflags"])

examples/project.janet

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
(setdyn *build-type* :release)
1818

19-
# get lflags from jfltk module?
20-
(def- lflags jfltk/lflags)
21-
2219
(def- examples
2320
(do
2421
(var files @[])
@@ -33,5 +30,4 @@
3330
(declare-executable
3431
:name (string/replace ".janet" "" fname)
3532
:entry fname
36-
:libs lflags
3733
))

project.janet

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)