Skip to content

Use static library when necessary #8

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions imgui.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ skipDirs = @["tests"]

requires "nim >= 1.0.0" # 1.0.0 promises that it will have backward compatibility

before install:
exec("nim c -r makecimgui.nim")

task gen, "Generate bindings from source":
exec("nim c -r tools/generator.nim")

Expand All @@ -23,3 +26,4 @@ task ci, "Create window with imgui null demo":
requires "nimgl@#1.0" # Please https://github.com/nim-lang/nimble/issues/482
exec("nim c -r tests/tnull.nim") # requires cimgui.dll
exec("nim cpp -r tests/tnull.nim")

22 changes: 22 additions & 0 deletions makecimgui.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import std/[os, strutils]
#import system
# import system/io

var (dir, name, ext) = splitFile(currentSourcePath())
dir = dir.joinPath("./src/imgui/private/cimgui")
#echo dir
setCurrentDir(dir)


proc exec(s: string) =
assert 0 == os.execShellCmd(s)

if fileExists("libcimgui.a"):
echo("Warning: libcimgui.a already built. Skipping.")
quit()

if "-fno-threadsafe-statics" notin open("Makefile").readAll():
exec("git apply ../0001-Remove-threadsafe.patch")

exec("make clean")
exec("make static")
48 changes: 30 additions & 18 deletions src/imgui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,35 @@ proc currentSourceDir(): string {.compileTime.} =
result = currentSourcePath().replace("\\", "/")
result = result[0 ..< result.rfind("/")]

{.passC: "-I" & currentSourceDir() & "/imgui/private/cimgui" & " -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1".}
const cimgui_dir = currentSourceDir() & "/imgui/private/cimgui"

{.passC: "-I" & cimgui_dir & " -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1".}
when defined(linux):
{.passL: "-Xlinker -rpath .".}

when not defined(cpp) or defined(cimguiDLL):
when defined(windows):
const imgui_dll* = "cimgui.dll"
elif defined(macosx):
const imgui_dll* = "cimgui.dylib"
when defined(c):
{.pragma: imgui_header, header: currentSourceDir() & "/imgui/private/ncimgui_c.h".}
when defined(cimguiDLL):
{.fatal: "not supported yet".}
when defined(windows):
const cimguiDLL {.strdefine.}: string = "cimgui.dll"
elif defined(macosx):
const cimguiDLL {.strdefine.}: string = "cimgui.dylib"
else:
const cimguiDLL {.strdefine.}: string = "cimgui.so"
const imgui_dll* = cimguiDLL
else:
const imgui_dll* = "cimgui.so"
{.passC: "-DCIMGUI_DEFINE_ENUMS_AND_STRUCTS".}
{.pragma: imgui_header, header: "cimgui.h".}
else:
{.compile: "imgui/private/cimgui/cimgui.cpp",
compile: "imgui/private/cimgui/imgui/imgui.cpp",
compile: "imgui/private/cimgui/imgui/imgui_draw.cpp",
compile: "imgui/private/cimgui/imgui/imgui_tables.cpp",
compile: "imgui/private/cimgui/imgui/imgui_widgets.cpp",
compile: "imgui/private/cimgui/imgui/imgui_demo.cpp".}
# TODO:
# run "makecimgui.nim" before install
{.passL: cimgui_dir & "/libcimgui.a".}

when defined(cpp):
{.compile: cimgui_dir & "/cimgui.cpp",
compile: cimgui_dir & "/imgui/imgui.cpp",
compile: cimgui_dir & "/imgui/imgui_draw.cpp",
compile: cimgui_dir & "/imgui/imgui_tables.cpp",
compile: cimgui_dir & "/imgui/imgui_widgets.cpp",
compile: cimgui_dir & "/imgui/imgui_demo.cpp".}
{.pragma: imgui_header, header: currentSourceDir() & "/imgui/private/ncimgui.h".}

# Enums
Expand Down Expand Up @@ -1925,8 +1934,11 @@ type

# Procs
{.push warning[HoleEnumConv]: off.}
when not defined(cpp) or defined(cimguiDLL):
{.push dynlib: imgui_dll, cdecl, discardable.}
when not defined(cpp):
when defined(cimguiDLL):
{.push dynlib: imgui_dll, cdecl, discardable.}
else:
{.push nodecl, discardable.}
else:
{.push nodecl, discardable.}

Expand Down
22 changes: 22 additions & 0 deletions src/imgui/private/0001-Remove-threadsafe.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Subject: [PATCH] Remove threadsafe

---
Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index d5462fc..90cc096 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ OBJS += ./imgui/imgui_demo.o
OBJS += ./imgui/imgui_tables.o
OBJS += ./imgui/imgui_widgets.o

-CXXFLAGS=-O2 -fno-exceptions -fno-rtti
+CXXFLAGS=-O2 -fno-exceptions -fno-rtti -fno-threadsafe-statics

UNAME_S := $(shell uname -s)

--
2.36.1

2 changes: 2 additions & 0 deletions src/imgui/private/ncimgui_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui.h"