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

fix embedding example, continued #21299

Merged
merged 10 commits into from
Apr 15, 2017
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,15 @@ script:
install -pm755 usr/lib/lib${name}*.dylib* /tmp/julia/lib/julia/;
done;
fi
- make $BUILDOPTS NO_GIT=1 prefix=/tmp/julia -C examples
- cd .. && mv julia julia2
- /tmp/julia/bin/julia --precompiled=no -e 'true' &&
/tmp/julia/bin/julia-debug --precompiled=no -e 'true'
- /tmp/julia/bin/julia -e 'versioninfo()'
- export JULIA_CPU_CORES=2 && export JULIA_TEST_MAXRSS_MB=600 &&
cd /tmp/julia/share/julia/test &&
/tmp/julia/bin/julia --check-bounds=yes runtests.jl $TESTSTORUN &&
/tmp/julia/bin/julia --check-bounds=yes runtests.jl libgit2-online download pkg
/tmp/julia/bin/julia --check-bounds=yes runtests.jl libgit2-online download pkg embedding
- cd `dirname $TRAVIS_BUILD_DIR` && mv julia2 julia &&
rm -f julia/deps/scratch/libgit2-*/CMakeFiles/CMakeOutput.log
# uncomment the following if failures are suspected to be due to the out-of-memory killer
Expand Down
3 changes: 2 additions & 1 deletion Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ endif
USE_GPL_LIBS ?= 1

# Directories where said libraries get installed to
prefix ?= $(abspath julia-$(JULIA_COMMIT))
prefix ?= $(BUILDROOT)/julia-$(JULIA_COMMIT)
Copy link
Contributor

Choose a reason for hiding this comment

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

@vtjnash can you think of any reason not to make this change? Part of the problem here was when examples/Makefile included Make.inc, the prefix was getting set to examples/julia-$(JULIA_COMMIT) which of course nothing else built into

bindir := $(prefix)/bin
libdir := $(prefix)/lib
libexecdir := $(prefix)/libexec
Expand Down Expand Up @@ -219,6 +219,7 @@ libdir_rel := $(shell $(JULIAHOME)/contrib/relative_path.sh $(bindir) $(libdir))
build_private_libdir_rel := $(shell $(JULIAHOME)/contrib/relative_path.sh $(build_bindir) $(build_private_libdir))
private_libdir_rel := $(shell $(JULIAHOME)/contrib/relative_path.sh $(bindir) $(private_libdir))
datarootdir_rel := $(shell $(JULIAHOME)/contrib/relative_path.sh $(bindir) $(datarootdir))
libexecdir_rel := $(shell $(JULIAHOME)/contrib/relative_path.sh $(bindir) $(libexecdir))
docdir_rel := $(shell $(JULIAHOME)/contrib/relative_path.sh $(bindir) $(docdir))
sysconfdir_rel := $(shell $(JULIAHOME)/contrib/relative_path.sh $(bindir) $(sysconfdir))
includedir_rel := $(shell $(JULIAHOME)/contrib/relative_path.sh $(bindir) $(includedir))
Expand Down
16 changes: 10 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ all: debug release
# sort is used to remove potential duplicates
DIRS := $(sort $(build_bindir) $(build_depsbindir) $(build_libdir) $(build_private_libdir) $(build_libexecdir) $(build_includedir) $(build_includedir)/julia $(build_sysconfdir)/julia $(build_datarootdir)/julia $(build_man1dir))
ifneq ($(BUILDROOT),$(JULIAHOME))
BUILDDIRS := $(BUILDROOT) $(addprefix $(BUILDROOT)/,base src ui doc deps test test/perf)
BUILDDIRS := $(BUILDROOT) $(addprefix $(BUILDROOT)/,base src ui doc deps test test/perf examples examples/embedding)
BUILDDIRMAKE := $(addsuffix /Makefile,$(BUILDDIRS))
DIRS := $(DIRS) $(BUILDDIRS)
$(BUILDDIRMAKE): | $(BUILDDIRS)
Expand Down Expand Up @@ -62,12 +62,16 @@ $(BUILDROOT)/doc/_build/html/en/index.html: $(shell find $(BUILDROOT)/base $(BUI
CLEAN_TARGETS += clean-docdir
clean-docdir:
@-rm -fr $(abspath $(build_docdir))
$(build_prefix)/.examples: $(wildcard $(JULIAHOME)/examples/*.jl) $(shell find $(JULIAHOME)/examples/clustermanager)

$(build_prefix)/.examples: $(wildcard $(JULIAHOME)/examples/*.jl) \
$(shell find $(JULIAHOME)/examples/clustermanager)
@echo Copying in usr/share/doc/julia/examples
@-rm -fr $(build_docdir)/examples
@mkdir -p $(build_docdir)/examples
@cp -R $(JULIAHOME)/examples/*.jl $(build_docdir)/examples/
@cp -R $(JULIAHOME)/examples/Makefile $(build_docdir)/examples/
@cp -R $(JULIAHOME)/examples/clustermanager $(build_docdir)/examples/
@cp -R $(JULIAHOME)/examples/embedding $(build_docdir)/examples
@echo 1 > $@

julia-symlink: julia-ui-$(JULIA_BUILD_MODE)
Expand Down Expand Up @@ -335,16 +339,15 @@ install: $(build_depsbindir)/stringreplace $(BUILDROOT)/doc/_build/html/en/index
done

$(INSTALL_M) $(build_bindir)/julia* $(DESTDIR)$(bindir)/
-cp -a $(build_libexecdir) $(DESTDIR)$(prefix)
ifeq ($(OS),WINNT)
-$(INSTALL_M) $(build_bindir)/*.dll $(DESTDIR)$(bindir)/
-$(INSTALL_M) $(build_libdir)/libjulia.dll.a $(DESTDIR)$(libdir)/
-$(INSTALL_M) $(build_libdir)/libjulia-debug.dll.a $(DESTDIR)$(libdir)/
-$(INSTALL_M) $(build_bindir)/libopenlibm.dll.a $(DESTDIR)$(libdir)/
else
-cp -a $(build_libexecdir) $(DESTDIR)$(prefix)

# Copy over .dSYM directories directly
ifeq ($(OS),Darwin)
# Copy over .dSYM directories directly
-cp -a $(build_libdir)/*.dSYM $(DESTDIR)$(libdir)
-cp -a $(build_private_libdir)/*.dSYM $(DESTDIR)$(private_libdir)
endif
Expand Down Expand Up @@ -522,6 +525,7 @@ clean: | $(CLEAN_TARGETS)
@-$(MAKE) -C $(BUILDROOT)/src clean
@-$(MAKE) -C $(BUILDROOT)/ui clean
@-$(MAKE) -C $(BUILDROOT)/test clean
@-$(MAKE) -C $(BUILDROOT)/examples clean
-rm -f $(BUILDROOT)/julia
-rm -f $(BUILDROOT)/*.tar.gz
-rm -f $(build_depsbindir)/stringreplace \
Expand All @@ -548,7 +552,7 @@ distcleanall: cleanall
test testall testall1 test clean distcleanall cleanall clean-* \
run-julia run-julia-debug run-julia-release run \
install binary-dist light-source-dist.tmp light-source-dist \
dist full-source-dist source-dist
dist full-source-dist source-dist examples

test: check-whitespace $(JULIA_BUILD_MODE)
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/test default JULIA_BUILD_MODE=$(JULIA_BUILD_MODE)
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ build_script:
test_script:
- usr\bin\julia -e "versioninfo()"
- usr\bin\julia --precompiled=no -e "true"
- cd test && ..\usr\bin\julia --check-bounds=yes runtests.jl all &&
..\usr\bin\julia --check-bounds=yes runtests.jl libgit2-online download pkg
- cd julia-* && .\bin\julia.exe --check-bounds=yes share\julia\test\runtests.jl all &&
.\bin\julia.exe --check-bounds=yes share\julia\test\runtests.jl libgit2-online download pkg embedding
2 changes: 2 additions & 0 deletions base/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ endif
@echo "const DATAROOTDIR = \"$(datarootdir_rel)\"" >> $@
@echo "const DOCDIR = \"$(docdir_rel)\"" >> $@
@echo "const LIBDIR = \"$(libdir_rel)\"" >> $@
@echo "const PRIVATE_LIBDIR = \"$(private_libdir_rel)\"" >> $@
@echo "const LIBEXECDIR = \"$(libexecdir_rel)\"" >> $@
@echo "const INCLUDEDIR = \"$(includedir_rel)\"" >> $@

@# This to ensure that we always rebuild this file, but only when it is modified do we touch build_h.jl,
Expand Down
16 changes: 12 additions & 4 deletions contrib/julia-config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function libDir()
end
end

private_libDir() = joinpath(JULIA_HOME, Base.PRIVATE_LIBDIR)

function includeDir()
joinpath(match(r"(.*)(bin)",JULIA_HOME).captures[1],"include","julia")
end
Expand All @@ -49,7 +51,13 @@ function initDir()
end

function ldflags()
replace("""-L$(libDir())""","\\","\\\\")
fl = replace("""-L$(libDir())""","\\","\\\\")
if is_windows()
fl = fl * " -Wl,--stack,8388608"
elseif is_linux()
fl = fl * " -Wl,--export-dynamic"
end
return fl
end

function ldlibs()
Expand All @@ -59,7 +67,7 @@ function ldlibs()
"julia"
end
if is_unix()
return replace("""-Wl,-rpath,$(libDir()) -l$libname""","\\","\\\\")
return replace("""-Wl,-rpath,$(libDir()) -Wl,-rpath,$(private_libDir()) -l$libname""","\\","\\\\")
else
return "-l$libname -lopenlibm"
end
Expand All @@ -70,9 +78,9 @@ function cflags()
arg2 = replace(includeDir(),"\\","\\\\")
threading_def = threadingOn() ? "-DJULIA_ENABLE_THREADING=1 " : ""
if is_unix()
return """$(threading_def)-fPIC -DJULIA_INIT_DIR=\\"$arg1\\" -I$arg2"""
return """-std=gnu99 $(threading_def)-fPIC -DJULIA_INIT_DIR=\\"$arg1\\" -I$arg2"""
else
return """$(threading_def)-DJULIA_INIT_DIR=\\"$arg1\\" -I$arg2"""
return """-std=gnu99 $(threading_def)-DJULIA_INIT_DIR=\\"$arg1\\" -I$arg2"""
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I looked into why we can't use c99 here. it looks like the difference between these options is in the compiler flags that get set (https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html). In particular, to use c99, we would need to set _POSIX_C_SOURCE >= 2001xxL (for a couple of pthread structs in the libuv header). When we get rid of the uv.h installation requirement (#8880), we'll only need to define _POSIX_SOURCE somewhere (for sigjmp_buf visibility).

end
end

Expand Down
5 changes: 3 additions & 2 deletions contrib/windows/msys_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ fi
echo 'FORCE_ASSERTIONS = 1' >> Make.user

cat Make.user
make -j3 VERBOSE=1
make -j3 VERBOSE=1 install
cp usr/bin/busybox.exe julia-*/bin
make build-stats
#make debug
make VERBOSE=1 -C examples
17 changes: 7 additions & 10 deletions doc/src/manual/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ We start with a simple C program that initializes Julia and calls some Julia cod
int main(int argc, char *argv[])
{
/* required: setup the Julia context */
jl_init(NULL);
jl_init();

/* run Julia commands */
jl_eval_string("print(sqrt(2.0))");
Expand Down Expand Up @@ -48,9 +48,9 @@ The file `ui/repl.c` program is another simple example of how to set `jl_options
linking against `libjulia`.

The first thing that has to be done before calling any other Julia C function is to initialize
Julia. This is done by calling `jl_init`, which takes as argument a C string (`const char*`) to
the location where Julia is installed. When the argument is `NULL`, Julia tries to determine the
install location automatically.
Julia. This is done by calling `jl_init`, which tries to automatically determine Julia's install
location. If you need to specify a custom location, or specify which system image to load,
use `jl_init_with_image` instead.

The second statement in the test program evaluates a Julia statement using a call to `jl_eval_string`.

Expand All @@ -63,8 +63,8 @@ example program calls this before returning from `main`.

```
>>> julia=CDLL('./libjulia.dylib',RTLD_GLOBAL)
>>> julia.jl_init.argtypes = [c_char_p]
>>> julia.jl_init('.')
>>> julia.jl_init.argtypes = []
>>> julia.jl_init()
250593296
```

Expand All @@ -83,15 +83,12 @@ shared data directory.

#### Example

Below is essentially the same as above with one small change; the argument to `jl_init` is now
**JULIA_INIT_DIR** which is defined by *julia-config.jl*.:

```
#include <julia.h>

int main(int argc, char *argv[])
{
jl_init(JULIA_INIT_DIR);
jl_init();
(void)jl_eval_string("println(sqrt(2.0))");
jl_atexit_hook(0);
return 0;
Expand Down
48 changes: 16 additions & 32 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
JULIAHOME := $(abspath ..)
SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
BUILDDIR := .
JULIAHOME := $(abspath $(SRCDIR)/..)
include $(JULIAHOME)/Make.inc

FLAGS = -Wall -Wno-strict-aliasing -fno-omit-frame-pointer \
-I$(JULIAHOME)/src -I$(JULIAHOME)/src/support -I$(build_includedir) $(CFLAGS)
outdir := $(libexecdir)

DEBUGFLAGS += $(FLAGS)
SHIPFLAGS += $(FLAGS)
JLDFLAGS += $(LDFLAGS) $(NO_WHOLE_ARCHIVE) $(call exec,$(LLVM_CONFIG) --ldflags) $(OSLIBS) $(RPATH)
embedding_binary := $(abspath $(outdir)/embedding$(JULIA_LIBSUFFIX)$(EXE))

ifeq ($(USE_SYSTEM_LIBM),0)
ifneq ($(UNTRUSTED_SYSTEM_LIBM),0)
JLDFLAGS += $(WHOLE_ARCHIVE) $(build_libdir)/libopenlibm.a $(NO_WHOLE_ARCHIVE)
endif
endif
release: embedding
debug: embedding-debug

embedding-release: embedding
embedding: $(embedding_binary)
embedding-debug: $(embedding_binary)

release debug:
$(MAKE) embedding-$@
$(embedding_binary): $(wildcard embedding/*)
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/examples/embedding $(JULIA_BUILD_MODE) \
JULIA="$(bindir)/julia$(JULIA_LIBSUFFIX)$(EXE)" BIN="$(outdir)" \
SPAWN="$(spawn)" CC="$(CC)"

%.o: %.c
@$(call PRINT_CC, $(CC) $(CPPFLAGS) $(CFLAGS) $(SHIPFLAGS) -c $< -o $@)
%.do: %.c
@$(call PRINT_CC, $(CC) $(CPPFLAGS) $(CFLAGS) $(DEBUGFLAGS) -c $< -o $@)
clean:
-rm -f $(embedding_binary) $(embedding_binary)-debug

embedding: $(build_bindir)/embedding$(EXE)
embedding-debug: $(build_bindir)/embedding-debug$(EXE)

$(build_bindir)/embedding$(EXE): embedding.o
@$(call PRINT_LINK, $(CXX) $(LINK_FLAGS) $(SHIPFLAGS) $^ -o $@ -L$(build_private_libdir) -L$(build_shlibdir) -ljulia $(JLDFLAGS))
$(build_bindir)/embedding-debug$(EXE): embedding.do
@$(call PRINT_LINK, $(CXX) $(LINK_FLAGS) $(DEBUGFLAGS) $^ -o $@ -L$(build_private_libdir) -L$(build_shlibdir) -ljulia-debug $(JLDFLAGS))


clean: | $(CLEAN_TARGETS)
rm -f *.o *.do
rm -f $(build_bindir)/embedding-debug $(build_bindir)/embedding

.PHONY: clean release debug
.PHONY: all embedding clean

42 changes: 42 additions & 0 deletions examples/embedding/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This Makefile template requires the following variables to be set
# in the environment or on the command-line:
# JULIA: path to julia[.exe] executable
# BIN: binary build directory

ifndef JULIA
$(error "Please pass JULIA=[path of target julia binary], or set as environment variable!")
endif
ifndef BIN
$(error "Please pass BIN=[path of build directory], or set as environment variable!")
endif

#=============================================================================
# this source directory where embedding.c is located
SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))

# get the executable suffix, if any
EXE := $(suffix $(abspath $(JULIA)))

# get compiler and linker flags. (see: `contrib/julia-config.jl`)
CFLAGS += -lm $(shell $(SPAWN) $(JULIA) -e \
'include(joinpath(JULIA_HOME,Base.DATAROOTDIR,"julia","julia-config.jl"))' \
-- --cflags --ldflags --ldlibs)

DEBUGFLAGS += -g

#=============================================================================

release: $(BIN)/embedding$(EXE)
debug: $(BIN)/embedding-debug$(EXE)

$(BIN)/embedding$(EXE): $(SRCDIR)/embedding.c
$(CC) $^ -o $@ $(CFLAGS)

$(BIN)/embedding-debug$(EXE): $(SRCDIR)/embedding.c
$(CC) $^ -o $@ $(CFLAGS) $(DEBUGFLAGS)

clean:
@rm -f $(BIN)/embedding-debug$(EXE) $(BIN)/embedding$(EXE)

.PHONY: release debug clean

7 changes: 4 additions & 3 deletions examples/embedding.c → examples/embedding/embedding.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ double my_c_sqrt(double x)

int main()
{
jl_init(NULL);
jl_init();

{
// Simple running Julia code
Expand Down Expand Up @@ -49,7 +49,7 @@ int main()
{
// 1D arrays

jl_value_t* array_type = jl_apply_array_type( jl_float64_type, 1 );
jl_value_t* array_type = jl_apply_array_type( (jl_value_t*)jl_float64_type, 1 );
jl_array_t* x = jl_alloc_array_1d(array_type , 10);
JL_GC_PUSH1(&x);

Expand Down Expand Up @@ -94,9 +94,10 @@ int main()
jl_eval_string("this_function_does_not_exist()");

if (jl_exception_occurred()) {
jl_show(jl_stderr_obj(), jl_exception_occurred());
jl_call2(jl_get_function(jl_base_module, "show"), jl_stderr_obj(), jl_exception_occurred());
jl_printf(jl_stderr_stream(), "\n");
}

}

int ret = 0;
Expand Down
5 changes: 4 additions & 1 deletion src/dlload.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ static void *jl_load_dynamic_library_(const char *modname, unsigned flags, int t
jl_error("could not load base module");
}
#else
handle = dlopen(NULL, RTLD_NOW);
Dl_info info;
if (!dladdr(&jl_load_dynamic_library, &info) || !info.dli_fname)
jl_error("could not load base module");
handle = dlopen(info.dli_fname, RTLD_NOW);
#endif
goto done;
}
Expand Down
14 changes: 8 additions & 6 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ void jl_get_builtins(void);

JL_DLLEXPORT void *jl_dl_handle;
void *jl_RTLD_DEFAULT_handle;
#ifdef _OS_WINDOWS_
JL_DLLEXPORT void *jl_exe_handle;
#ifdef _OS_WINDOWS_
void *jl_ntdll_handle;
void *jl_kernel32_handle;
void *jl_crtdll_handle;
Expand Down Expand Up @@ -541,11 +541,6 @@ void _julia_init(JL_IMAGE_SEARCH rel)
jl_arr_xtralloc_limit = total_mem / 100; // Extra allocation limited to 1% of total RAM
jl_find_stack_bottom();
jl_dl_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT);
#ifdef RTLD_DEFAULT
jl_RTLD_DEFAULT_handle = RTLD_DEFAULT;
#else
jl_RTLD_DEFAULT_handle = jl_dl_handle;
#endif
#ifdef _OS_WINDOWS_
jl_ntdll_handle = jl_dlopen("ntdll.dll", 0); // bypass julia's pathchecking for system dlls
jl_kernel32_handle = jl_dlopen("kernel32.dll", 0);
Expand All @@ -564,6 +559,13 @@ void _julia_init(JL_IMAGE_SEARCH rel)
HMODULE jl_dbghelp = (HMODULE) jl_dlopen("dbghelp.dll", 0);
if (jl_dbghelp)
hSymRefreshModuleList = (BOOL (WINAPI*)(HANDLE)) jl_dlsym(jl_dbghelp, "SymRefreshModuleList");
#else
jl_exe_handle = jl_dlopen(NULL, JL_RTLD_NOW);
#ifdef RTLD_DEFAULT
jl_RTLD_DEFAULT_handle = RTLD_DEFAULT;
#else
jl_RTLD_DEFAULT_handle = jl_exe_handle;
#endif
#endif

#if defined(JL_USE_INTEL_JITEVENTS)
Expand Down
Loading