Skip to content

Commit

Permalink
add: make examples
Browse files Browse the repository at this point in the history
  • Loading branch information
XiangYyang committed Jul 23, 2023
1 parent 0cf9e6d commit 605497c
Show file tree
Hide file tree
Showing 35 changed files with 119 additions and 63 deletions.
50 changes: 46 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ BUILD_DIR = build
# Test path
TEST_DIR = tests

# Example path
EXAMPLE_DIR = examples

# 构建输出
ifdef MTFMT_BUILD_OUTPUT_DIR
OUTPUT_DIR = $(MTFMT_BUILD_OUTPUT_DIR)
Expand All @@ -91,6 +94,14 @@ $(wildcard ./thirds/unity/src/*.c)
TEST_CPP_SOURCES= \
$(wildcard ./tests/*.cpp)

# 例子 (C)
EXAMPLE_C_SOURCES = \
$(wildcard ./examples/*.c)

# 例子 (C++)
EXAMPLE_CPP_SOURCES = \
$(wildcard ./examples/*.cpp)

# 编译器
ifdef MTFMT_BUILD_GCC_PREFIX
PREFIX = $(MTFMT_BUILD_GCC_PREFIX)
Expand Down Expand Up @@ -177,6 +188,16 @@ ifeq ($(MTFMT_BUILD_INC_ADDITIONAL_OUT), 1)
DYLIB_LD_OPTS += -Wl,--output-def,$(OUTPUT_DIR)/$(TARGET_NAME).def,--out-implib,$(OUTPUT_DIR)/$(DYLIB_IMPLIB_TARGET)
endif

# 例子的链接选项
EXAMPLE_LD_OPTS =

ifneq ($(MTFMT_BUILD_DEBUG), 1)
EXAMPLE_LD_OPTS += $(LTO_OPT)
endif

# 回收不需要的段
EXAMPLE_LD_OPTS += -Wl,--gc-sections

# 编译测试文件的链接选项
TEST_LD_OPTS =

Expand Down Expand Up @@ -204,12 +225,24 @@ vpath %.c $(sort $(dir $(C_SOURCES)))
TEST_OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(TEST_C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(TEST_C_SOURCES)))

# list of cpp objects
# list of cpp objects for tests
TEST_OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(TEST_CPP_SOURCES:.cpp=.o)))
vpath %.cpp $(sort $(dir $(TEST_CPP_SOURCES)))

# list of objects for examples
EXAMPLE_OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(EXAMPLE_C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(EXAMPLE_C_SOURCES)))

# list of objects for cpp examples
EXAMPLE_OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(EXAMPLE_CPP_SOURCES:.cpp=.o)))
vpath %.cpp $(sort $(dir $(EXAMPLE_CPP_SOURCES)))

# list of examples
EXAMPLE_TARGET_LIST = $(addprefix $(OUTPUT_DIR)/,$(notdir $(EXAMPLE_C_SOURCES:.c=$(EXE_EXT))))
EXAMPLE_TARGET_LIST += $(addprefix $(OUTPUT_DIR)/,$(notdir $(EXAMPLE_CPP_SOURCES:.cpp=$(EXE_EXT))))

# build all
all: lib dylib test
all: lib dylib test examples
@echo Build completed.

# build static lib
Expand All @@ -218,7 +251,12 @@ lib: $(OUTPUT_DIR)/$(LIB_TARGET)

# build dylib
dylib: $(OUTPUT_DIR)/$(DYLIB_TARGET)
@echo Link completed.
@echo Build dymatic library completed.

# 例子
examples: $(EXAMPLE_TARGET_LIST)
@echo All examples: $(EXAMPLE_TARGET_LIST)
@echo Build all examples completed.

# 测试
test: $(OUTPUT_DIR)/$(TEST_TARGET)
Expand Down Expand Up @@ -253,7 +291,11 @@ $(OUTPUT_DIR)/$(DYLIB_TARGET): $(OBJECTS) Makefile | $(OUTPUT_DIR)

$(OUTPUT_DIR)/$(TEST_TARGET): $(OBJECTS) $(TEST_OBJECTS) | $(OUTPUT_DIR)
@echo $(LD_DISPLAY) $@
@gcc $(OBJECTS) $(TEST_OBJECTS) $(TEST_LD_OPTS) -o $@
@$(CC) $(OBJECTS) $(TEST_OBJECTS) $(TEST_LD_OPTS) -o $@

$(OUTPUT_DIR)/example_%$(EXE_EXT): $(EXAMPLE_OBJECTS) $(OBJECTS) | $(OUTPUT_DIR)
@gcc $(OBJECTS) "$(BUILD_DIR)/$(notdir $(basename $@)).o" $(EXAMPLE_LD_OPTS) -o $@
@echo Build example target "$@"

$(BUILD_DIR):
mkdir $@
Expand Down
4 changes: 4 additions & 0 deletions examples/example_cpp_str.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

int main(void)
{
}
6 changes: 6 additions & 0 deletions examples/example_fmt_int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#include "mtfmt.h"

int main(void)
{
}
4 changes: 4 additions & 0 deletions examples/example_fmt_repl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

int main(void)
{
}
4 changes: 2 additions & 2 deletions tests/test_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mm_heap.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cimpl_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mm_parser.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_align.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mm_fmt.h"
#include "mm_heap.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mm_fmt.h"
#include "mm_heap.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_behav.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mm_fmt.h"
#include "mm_heap.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_chrono.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_escape.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mm_fmt.h"
#include "mm_heap.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_sty.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mm_fmt.h"
#include "mm_heap.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_type_int_arr.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmt_type_int_val.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.h"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/helper.h → tests/test_helper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: LGPL-3.0
/**
* @file helper.h
* @file test_helper.h
* @author 向阳 (hinata.hoshino@foxmail.com)
* @brief 测试相关的helper
* @version 1.0
Expand Down
4 changes: 2 additions & 2 deletions tests/test_into_fix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.hpp"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_into_fp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.hpp"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_into_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.hpp"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/main.c → tests/test_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "main.h"
#include "helper.h"
#include "test_main.h"
#include "mtfmt.h"
#include "test_helper.h"
#include "unity.h"
#include <stddef.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/main.h → tests/test_main.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: LGPL-3.0
/**
* @file main.h
* @file test_main.h
* @author 向阳 (hinata.hoshino@foxmail.com)
* @brief 所有可用的测试
* @version 1.0
Expand Down
4 changes: 2 additions & 2 deletions tests/test_monad_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "test_helper.h"
#include "test_main.h"
#include "mtfmt.hpp"
#include "unity.h"

Expand Down
2 changes: 1 addition & 1 deletion tests/test_monad_oper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "main.h"
#include "mtfmt.hpp"
#include "test_main.h"
#include "unity.h"
#include <exception>
#include <stddef.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/test_string_append.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.hpp"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stdio.h>

Expand Down
4 changes: 2 additions & 2 deletions tests/test_string_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.hpp"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stdio.h>

Expand Down
4 changes: 2 additions & 2 deletions tests/test_string_concat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @copyright Copyright (c) 向阳, all rights reserved.
*
*/
#include "helper.h"
#include "main.h"
#include "mtfmt.hpp"
#include "test_helper.h"
#include "test_main.h"
#include "unity.h"
#include <stdio.h>

Expand Down
Loading

0 comments on commit 605497c

Please sign in to comment.