diff --git a/samples/static_lib/Makefile b/samples/static_lib/Makefile new file mode 100644 index 00000000000..5ae528b948f --- /dev/null +++ b/samples/static_lib/Makefile @@ -0,0 +1,38 @@ +# +# Copyright (c) 2016 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +MYLIB = mylib/lib/libmylib.a + +all: + make clean + make mylib + make hello_world + +hello_world: $(MYLIB) + make -C hello_world + +$(MYLIB): + make -C mylib + +run: + make -C hello_world qemu + +pristine: + make -C mylib clean + make -C hello_world pristine + +clean: + make pristine diff --git a/samples/static_lib/README b/samples/static_lib/README new file mode 100644 index 00000000000..ae79223d05c --- /dev/null +++ b/samples/static_lib/README @@ -0,0 +1,30 @@ +Linking with a static library +############################# + +This sample shows how to link a static library to a Zephyr application. +A sample library is also included. + +Read mylib/Makefile to discover how to use the Zephyr's toolchain +to build a static library. + +If the library's source code is available, perhaps it could be more +easier to integrate that source code to your application than creating +the static library. + + +Build instructions +================== + +1. It is assumed that ZEPHYR_GCC_VARIANT and ZEPHYR_SDK_INSTALL_DIR +variables are already set. See: + +https://www.zephyrproject.org/doc/getting_started/getting_started.html + +2. source $ZEPHYR/zephyr-env.sh + + Where $ZEPHYR points to the directory that contains the zephyr + repository. + +3. make + +4. make run diff --git a/samples/static_lib/hello_world/Kbuild b/samples/static_lib/hello_world/Kbuild new file mode 100644 index 00000000000..5e4f769a4b9 --- /dev/null +++ b/samples/static_lib/hello_world/Kbuild @@ -0,0 +1,3 @@ +subdir-ccflags-y += -I$(SOURCE_DIR)/../mylib/include + +obj-y += src/ diff --git a/samples/static_lib/hello_world/Makefile b/samples/static_lib/hello_world/Makefile new file mode 100644 index 00000000000..3a9372e5e23 --- /dev/null +++ b/samples/static_lib/hello_world/Makefile @@ -0,0 +1,25 @@ +# +# Copyright (c) 2016 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +KERNEL_TYPE = nano +BOARD = qemu_x86 +CONF_FILE = prj.conf + +export SOURCE_DIR = $(ZEPHYR_BASE)/samples/static_lib/hello_world +export LDFLAGS_zephyr += -L$(CURDIR)/../mylib/lib +export ALL_LIBS += mylib + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/samples/static_lib/hello_world/prj.conf b/samples/static_lib/hello_world/prj.conf new file mode 100644 index 00000000000..8b042db5406 --- /dev/null +++ b/samples/static_lib/hello_world/prj.conf @@ -0,0 +1 @@ +CONFIG_STDOUT_CONSOLE=y diff --git a/samples/static_lib/hello_world/src/Makefile b/samples/static_lib/hello_world/src/Makefile new file mode 100644 index 00000000000..1c10941fdd7 --- /dev/null +++ b/samples/static_lib/hello_world/src/Makefile @@ -0,0 +1,17 @@ +# +# Copyright (c) 2016 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +obj-y += main.o diff --git a/samples/static_lib/hello_world/src/main.c b/samples/static_lib/hello_world/src/main.c new file mode 100644 index 00000000000..cbb84c4ec25 --- /dev/null +++ b/samples/static_lib/hello_world/src/main.c @@ -0,0 +1,29 @@ +/* hello world example: calling functions from a static library */ + +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include + +void main(void) +{ + printf("Hello World!\n"); + mylib_hello_world(); +} + diff --git a/samples/static_lib/mylib/Makefile b/samples/static_lib/mylib/Makefile new file mode 100644 index 00000000000..4811c02334d --- /dev/null +++ b/samples/static_lib/mylib/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2016 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +include $(ZEPHYR_BASE)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT) + +# This Makefile is like a replacement of the $(ZEPHYR_BASE)/Makefile +# for libraries. So, we need to define ARCH here. +ARCH ?= x86 + +CROSS_COMPILE = $(CROSS_COMPILE_$(ARCH)) +TOOLCHAIN_CFLAGS = $(TOOLCHAIN_CFLAGS_$(ARCH)) + +CC = $(CROSS_COMPILE)gcc +AR = $(CROSS_COMPILE)ar + +all: + mkdir -p obj lib + $(CC) -c $(TOOLCHAIN_CFLAGS) -Iinclude src/mylib.c -o obj/mylib.o + $(AR) -rcs lib/libmylib.a obj/mylib.o + +clean: + rm -rf obj lib + diff --git a/samples/static_lib/mylib/include/mylib.h b/samples/static_lib/mylib/include/mylib.h new file mode 100644 index 00000000000..6299b2caeca --- /dev/null +++ b/samples/static_lib/mylib/include/mylib.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _MYLIB_H_ +#define _MYLIB_H_ + +int mylib_hello_world(void); + +#endif diff --git a/samples/static_lib/mylib/src/mylib.c b/samples/static_lib/mylib/src/mylib.c new file mode 100644 index 00000000000..1663bced9d3 --- /dev/null +++ b/samples/static_lib/mylib/src/mylib.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mylib.h" +#include + +int mylib_hello_world(void) +{ + printf("mylib says: Hello World!"); + return 0; +}