samples: external_lib: Build binaries in the build directory

This fixes https://github.com/zephyrproject-rtos/zephyr/issues/4925

Signed-off-by: Sebastian Boe <sebastian.boe@nordicsemi.no>
diff --git a/samples/application_development/external_lib/CMakeLists.txt b/samples/application_development/external_lib/CMakeLists.txt
index 42f0fb3..48db617 100644
--- a/samples/application_development/external_lib/CMakeLists.txt
+++ b/samples/application_development/external_lib/CMakeLists.txt
@@ -25,21 +25,23 @@
 # Add an external project to be able download and build the third
 # party library. In this case downloading is not necessary as it has
 # been committed to the repository.
-set(mylib_dir ${CMAKE_CURRENT_SOURCE_DIR}/mylib)
+set(mylib_src_dir   ${CMAKE_CURRENT_SOURCE_DIR}/mylib)
+set(mylib_build_dir ${CMAKE_CURRENT_BINARY_DIR}/mylib)
 ExternalProject_Add(
-  mylib_project           # Name for custom target
-  PREFIX     ${mylib_dir} # Root dir for entire project
-  SOURCE_DIR ${mylib_dir}
-  BINARY_DIR ${mylib_dir} # This particular build system is invoked from the root
+  mylib_project                 # Name for custom target
+  PREFIX     ${mylib_build_dir} # Root dir for entire project
+  SOURCE_DIR ${mylib_src_dir}
+  BINARY_DIR ${mylib_src_dir} # This particular build system is invoked from the root
   CONFIGURE_COMMAND ""    # Skip configuring the project, e.g. with autoconf
   BUILD_COMMAND
   make
+  PREFIX=${mylib_build_dir}
   CC=${CMAKE_C_COMPILER}
   CFLAGS=${external_project_cflags}
   INSTALL_COMMAND ""      # This particular build system has no install command
   )
-set(MYLIB_INCLUDE_DIR ${mylib_dir}/include)
-set(MYLIB_LIB_DIR     ${mylib_dir}/lib)
+set(MYLIB_INCLUDE_DIR ${mylib_src_dir}/include)
+set(MYLIB_LIB_DIR     ${mylib_build_dir}/lib)
 
 # Create a wrapper CMake library that our app can link with
 add_library(mylib_lib STATIC IMPORTED)
diff --git a/samples/application_development/external_lib/mylib/Makefile b/samples/application_development/external_lib/mylib/Makefile
index 1ce3f00..d8da6a7 100644
--- a/samples/application_development/external_lib/mylib/Makefile
+++ b/samples/application_development/external_lib/mylib/Makefile
@@ -4,11 +4,14 @@
 # SPDX-License-Identifier: Apache-2.0
 #
 
+PREFIX ?= .
+OBJ_DIR ?= $(PREFIX)/obj
+LIB_DIR ?= $(PREFIX)/lib
+
 all:
-	mkdir -p obj lib
-	$(CC) -c $(CFLAGS) -Iinclude src/mylib.c -o obj/mylib.o
-	$(AR) -rcs lib/libmylib.a obj/mylib.o
+	mkdir -p $(OBJ_DIR) $(LIB_DIR)
+	$(CC) -c $(CFLAGS) -Iinclude src/mylib.c -o $(OBJ_DIR)/mylib.o
+	$(AR) -rcs $(LIB_DIR)/libmylib.a $(OBJ_DIR)/mylib.o
 
 clean:
-	rm -rf obj lib
-
+	rm -rf $(OBJ_DIR) $(LIB_DIR)