From 916ee9b5beb9f8524b48be7f36186ce52cf66986 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Fri, 13 Sep 2019 13:05:29 -0400 Subject: [PATCH] Introduce unity_setup_stubs.h and eliminate UNITY_INCLUDE_SETUP_STUBS. Requiring a macro like UNITY_INCLUDE_SETUP_STUBS to be defined before including unity.h is fragile and does not work if unity.h is included (directly or indirectly) via a command-line directive. This also resolves issue #438. --- auto/generate_test_runner.rb | 4 +++- src/unity.c | 1 + src/unity.h | 24 ++----------------- src/unity_setup_stubs.h | 46 ++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 src/unity_setup_stubs.h diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index 6dc90e05..59b649b3 100644 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -169,8 +169,10 @@ def create_header(output, mocks, testfile_includes = []) output.puts('/* AUTOGENERATED FILE. DO NOT EDIT. */') create_runtest(output, mocks) output.puts("\n/*=======Automagically Detected Files To Include=====*/") - output.puts('#define UNITY_INCLUDE_SETUP_STUBS') if @options[:suite_setup].nil? output.puts("#include \"#{@options[:framework]}.h\"") + output.puts('#ifdef _WIN32') + output.puts("#include \"#{@options[:framework]}_setup_stubs.h\"") + output.puts('#endif') output.puts('#include "cmock.h"') unless mocks.empty? output.puts('#ifndef UNITY_EXCLUDE_SETJMP_H') output.puts('#include ') diff --git a/src/unity.c b/src/unity.c index 9883fd75..2d82ef0d 100644 --- a/src/unity.c +++ b/src/unity.c @@ -5,6 +5,7 @@ ============================================================================ */ #include "unity.h" +#include "unity_setup_stubs.h" #include #ifdef AVR diff --git a/src/unity.h b/src/unity.h index 8f1fe2e5..dc022b9b 100644 --- a/src/unity.h +++ b/src/unity.h @@ -29,28 +29,8 @@ void tearDown(void); void suiteSetUp(void); int suiteTearDown(int num_failures); -/* If the compiler supports it, the following block provides stub - * implementations of the above functions as weak symbols. Note that on - * some platforms (MinGW for example), weak function implementations need - * to be in the same translation unit they are called from. This can be - * achieved by defining UNITY_INCLUDE_SETUP_STUBS before including unity.h. */ -#ifdef UNITY_INCLUDE_SETUP_STUBS - #ifdef UNITY_WEAK_ATTRIBUTE - UNITY_WEAK_ATTRIBUTE void setUp(void) { } - UNITY_WEAK_ATTRIBUTE void tearDown(void) { } - UNITY_WEAK_ATTRIBUTE void suiteSetUp(void) { } - UNITY_WEAK_ATTRIBUTE int suiteTearDown(int num_failures) { return num_failures; } - #elif defined(UNITY_WEAK_PRAGMA) - #pragma weak setUp - void setUp(void) { } - #pragma weak tearDown - void tearDown(void) { } - #pragma weak suiteSetUp - void suiteSetUp(void) { } - #pragma weak suiteTearDown - int suiteTearDown(int num_failures) { return num_failures; } - #endif -#endif +/* Stubs of the above four functions are provided as weak symbols in + * unity_setup_stubs.h. */ /*------------------------------------------------------- * Configuration Options diff --git a/src/unity_setup_stubs.h b/src/unity_setup_stubs.h new file mode 100644 index 00000000..f43180ef --- /dev/null +++ b/src/unity_setup_stubs.h @@ -0,0 +1,46 @@ +/* ========================================== + Unity Project - A Test Framework for C + Copyright (c) 2007-19 Mike Karlesky, Mark VanderVoord, Greg Williams + [Released under MIT License. Please refer to license.txt for details] +========================================== */ + +#ifndef UNITY_SETUP_STUBS_H +#define UNITY_SETUP_STUBS_H + +#include "unity.h" + +/* If the compiler supports it, this header provides stub implementations + * of the following functions as weak symbols: + * + * - setUp() + * - tearDown() + * - suiteSetUp() + * - suiteTearDown() + * + * This header is always included by unity.c, which is sufficient when + * targeting GNU/Linux and other ELF targets. On some other platforms + * (MinGW for example), weak function implementations need to be in the + * same translation unit they are called from, so this header is also + * included by each generated test runner. + * + * If neither UNITY_WEAK_ATTRIBUTE nor UNITY_WEAK_PRAGMA is defined, the + * compiler is assumed to have no support for weak symbols, and this + * header has no effect. */ + +#ifdef UNITY_WEAK_ATTRIBUTE + UNITY_WEAK_ATTRIBUTE void setUp(void) { } + UNITY_WEAK_ATTRIBUTE void tearDown(void) { } + UNITY_WEAK_ATTRIBUTE void suiteSetUp(void) { } + UNITY_WEAK_ATTRIBUTE int suiteTearDown(int num_failures) { return num_failures; } +#elif defined(UNITY_WEAK_PRAGMA) + #pragma weak setUp + void setUp(void) { } + #pragma weak tearDown + void tearDown(void) { } + #pragma weak suiteSetUp + void suiteSetUp(void) { } + #pragma weak suiteTearDown + int suiteTearDown(int num_failures) { return num_failures; } +#endif + +#endif