diff --git a/Makefile b/Makefile index 1ae6bd991..945797181 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,18 @@ DESTDIR=/usr/local PREFIX=mbedtls_ +PROGRAMS_DIR=./programs +TESTS_DIR=./tests + +DIR_FOR_MBED_TLS_ENV=./library +ifneq "$(wildcard $(DIR_FOR_MBED_TLS_ENV) )" "" + LIBRARY_DIR=./library + INCLUDE_DIR=./include +else + LIBRARY_DIR=./src + INCLUDE_DIR=./inc +endif + .SILENT: .PHONY: all no_test programs lib tests install uninstall clean test check covtest lcov apidoc apidoc_clean @@ -12,26 +24,26 @@ all: programs tests no_test: programs programs: lib - $(MAKE) -C programs + $(MAKE) -C $(PROGRAMS_DIR) lib: - $(MAKE) -C library + $(MAKE) -C $(LIBRARY_DIR) tests: lib - $(MAKE) -C tests + $(MAKE) -C $(TESTS_DIR) ifndef WINDOWS install: no_test - mkdir -p $(DESTDIR)/include/mbedtls - cp -rp include/mbedtls $(DESTDIR)/include + mkdir -p $(DESTDIR)/$(INCLUDE_DIR)/mbedtls + cp -rp $(INCLUDE_DIR)/mbedtls $(DESTDIR)/$(INCLUDE_DIR) mkdir -p $(DESTDIR)/lib - cp -RP library/libmbedtls.* $(DESTDIR)/lib - cp -RP library/libmbedx509.* $(DESTDIR)/lib - cp -RP library/libmbedcrypto.* $(DESTDIR)/lib + cp -RP $(LIBRARY_DIR)/libmbedtls.* $(DESTDIR)/lib + cp -RP $(LIBRARY_DIR)/libmbedx509.* $(DESTDIR)/lib + cp -RP $(LIBRARY_DIR)/libmbedcrypto.* $(DESTDIR)/lib mkdir -p $(DESTDIR)/bin - for p in programs/*/* ; do \ + for p in $(PROGRAMS_DIR)/*/* ; do \ if [ -x $$p ] && [ ! -d $$p ] ; \ then \ f=$(PREFIX)`basename $$p` ; \ @@ -40,12 +52,12 @@ install: no_test done uninstall: - rm -rf $(DESTDIR)/include/mbedtls + rm -rf $(DESTDIR)/$(INCLUDE_DIR)/mbedtls rm -f $(DESTDIR)/lib/libmbedtls.* rm -f $(DESTDIR)/lib/libmbedx509.* rm -f $(DESTDIR)/lib/libmbedcrypto.* - for p in programs/*/* ; do \ + for p in $(PROGRAMS_DIR)/*/* ; do \ if [ -x $$p ] && [ ! -d $$p ] ; \ then \ f=$(PREFIX)`basename $$p` ; \ @@ -82,15 +94,15 @@ ifndef WINDOWS endif clean: - $(MAKE) -C library clean - $(MAKE) -C programs clean - $(MAKE) -C tests clean + $(MAKE) -C $(LIBRARY_DIR) clean + $(MAKE) -C $(PROGRAMS_DIR) clean + $(MAKE) -C $(TESTS_DIR) clean ifndef WINDOWS find . \( -name \*.gcno -o -name \*.gcda -o -name \*.info \) -exec rm {} + endif check: lib tests - $(MAKE) -C tests check + $(MAKE) -C $(TESTS_DIR) check test: check @@ -99,14 +111,14 @@ ifndef WINDOWS # make CFLAGS='--coverage -g3 -O0' covtest: $(MAKE) check - programs/test/selftest - tests/compat.sh - tests/ssl-opt.sh + $(PROGRAMS_DIR)/test/selftest + $(TESTS_DIR)/compat.sh + $(TESTS_DIR)/ssl-opt.sh lcov: rm -rf Coverage - lcov --capture --initial --directory library -o files.info - lcov --capture --directory library -o tests.info + lcov --capture --initial --directory $(LIBRARY_DIR) -o files.info + lcov --capture --directory $(LIBRARY_DIR) -o tests.info lcov --add-tracefile files.info --add-tracefile tests.info -o all.info lcov --remove all.info -o final.info '*.h' gendesc tests/Descriptions.txt -o descriptions @@ -122,7 +134,7 @@ apidoc_clean: endif ## Editor navigation files -C_SOURCE_FILES = $(wildcard include/*/*.h library/*.[hc] programs/*/*.[hc] tests/suites/*.function) +C_SOURCE_FILES = $(wildcard $(INCLUDE_DIR)/*/*.h $(LIBRARY_DIR)/*.[hc] $(PROGRAMS_DIR)/*/*.[hc] $(TESTS_DIR)/suites/*.function) tags: $(C_SOURCE_FILES) ctags -o $@ $(C_SOURCE_FILES) TAGS: $(C_SOURCE_FILES) diff --git a/library/Makefile b/library/Makefile index 341888afb..ce9c1eb99 100644 --- a/library/Makefile +++ b/library/Makefile @@ -5,7 +5,20 @@ CFLAGS ?= -O2 WARNING_CFLAGS ?= -Wall -W -Wdeclaration-after-statement LDFLAGS ?= -LOCAL_CFLAGS = $(WARNING_CFLAGS) -I../include -D_FILE_OFFSET_BITS=64 +DIR_FOR_MBED_TLS_ENV=../library +ifneq "$(wildcard $(DIR_FOR_MBED_TLS_ENV) )" "" + # Set include dirs for Mbed TLS test environment + INCLUDE_DIRS=-I../include +else + # Set include dirs for Mbed OS test environment + INCLUDE_DIRS=-I.. -I../inc +endif + +LOCAL_CFLAGS = $(WARNING_CFLAGS) $(INCLUDE_DIRS) -D_FILE_OFFSET_BITS=64 +ifdef MBEDTLS_CONFIG_FILE +LOCAL_CFLAGS += "-DMBEDTLS_CONFIG_FILE=\"${MBEDTLS_CONFIG_FILE}\"" +endif + LOCAL_LDFLAGS = ifdef DEBUG diff --git a/programs/Makefile b/programs/Makefile index 9b01e45cd..7ceca2eae 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -7,17 +7,32 @@ WARNING_CFLAGS ?= -Wall -W -Wdeclaration-after-statement WARNING_CXXFLAGS ?= -Wall -W LDFLAGS ?= -LOCAL_CFLAGS = $(WARNING_CFLAGS) -I../include -D_FILE_OFFSET_BITS=64 -LOCAL_CXXFLAGS = $(WARNING_CXXFLAGS) -I../include -D_FILE_OFFSET_BITS=64 -LOCAL_LDFLAGS = -L../library \ +# Check test environment. If ../library is available then Mbed TLS is used. +# Otherwise Mbed OS environment is used. +DIR_FOR_MBED_TLS_ENV=../library +ifneq "$(wildcard $(DIR_FOR_MBED_TLS_ENV) )" "" + LIBRARY_DIR=../library + INCLUDE_DIR=-I../include +else + LIBRARY_DIR=../src + INCLUDE_DIR=-I.. -I../inc +endif + +LOCAL_CFLAGS = $(WARNING_CFLAGS) $(INCLUDE_DIR) -D_FILE_OFFSET_BITS=64 +ifdef MBEDTLS_CONFIG_FILE +LOCAL_CFLAGS += "-DMBEDTLS_CONFIG_FILE=\"${MBEDTLS_CONFIG_FILE}\"" +endif + +LOCAL_CXXFLAGS = $(WARNING_CXXFLAGS) $(INCLUDE_DIR) -D_FILE_OFFSET_BITS=64 +LOCAL_LDFLAGS = -L$(LIBRARY_DIR) \ -lmbedtls$(SHARED_SUFFIX) \ -lmbedx509$(SHARED_SUFFIX) \ -lmbedcrypto$(SHARED_SUFFIX) ifndef SHARED -DEP=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a +DEP=$(LIBRARY_DIR)/libmbedcrypto.a $(LIBRARY_DIR)/libmbedx509.a $(LIBRARY_DIR)/libmbedtls.a else -DEP=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT) +DEP=$(LIBRARY_DIR)/libmbedcrypto.$(DLEXT) $(LIBRARY_DIR)/libmbedx509.$(DLEXT) $(LIBRARY_DIR)/libmbedtls.$(DLEXT) endif ifdef DEBUG @@ -47,12 +62,12 @@ ifdef ZLIB LOCAL_LDFLAGS += -lz endif -APPS = aes/aescrypt2$(EXEXT) aes/crypt_and_hash$(EXEXT) \ - hash/hello$(EXEXT) hash/generic_sum$(EXEXT) \ - pkey/dh_client$(EXEXT) \ +APPS = aes/aescrypt2$(EXEXT) aes/crypt_and_hash$(EXEXT) \ + hash/hello$(EXEXT) hash/generic_sum$(EXEXT) \ + pkey/dh_client$(EXEXT) \ pkey/dh_genprime$(EXEXT) pkey/dh_server$(EXEXT) \ - pkey/ecdh_curve25519$(EXEXT) \ - pkey/ecdsa$(EXEXT) pkey/gen_key$(EXEXT) \ + pkey/ecdh_curve25519$(EXEXT) \ + pkey/ecdsa$(EXEXT) pkey/gen_key$(EXEXT) \ pkey/key_app$(EXEXT) pkey/key_app_writer$(EXEXT) \ pkey/mpi_demo$(EXEXT) pkey/pk_decrypt$(EXEXT) \ pkey/pk_encrypt$(EXEXT) pkey/pk_sign$(EXEXT) \ @@ -65,12 +80,12 @@ APPS = aes/aescrypt2$(EXEXT) aes/crypt_and_hash$(EXEXT) \ ssl/ssl_server$(EXEXT) ssl/ssl_server2$(EXEXT) \ ssl/ssl_fork_server$(EXEXT) ssl/mini_client$(EXEXT) \ ssl/ssl_mail_client$(EXEXT) random/gen_entropy$(EXEXT) \ - random/gen_random_havege$(EXEXT) \ - random/gen_random_ctr_drbg$(EXEXT) \ + random/gen_random_havege$(EXEXT) \ + random/gen_random_ctr_drbg$(EXEXT) \ test/benchmark$(EXEXT) \ test/selftest$(EXEXT) test/udp_proxy$(EXEXT) \ - test/zeroize$(EXEXT) \ - test/query_compile_time_config$(EXEXT) \ + test/zeroize$(EXEXT) \ + test/query_compile_time_config$(EXEXT) \ util/pem2der$(EXEXT) util/strerror$(EXEXT) \ x509/cert_app$(EXEXT) x509/crl_app$(EXEXT) \ x509/cert_req$(EXEXT) x509/cert_write$(EXEXT) \ diff --git a/scripts/config.pl b/scripts/config.pl index 287f1f18b..edd4200d5 100755 --- a/scripts/config.pl +++ b/scripts/config.pl @@ -53,7 +53,28 @@ use warnings; use strict; -my $config_file = "include/mbedtls/config.h"; +my $config_file; +my $include_dir; +my $library_dir; +my $mbedtls_config_file = $ENV{"MBEDTLS_CONFIG_FILE"}; + +if ( ( -d "../library") || ( -d "./library") ) { + # library directory is present, we are using Mbed TLS namespace + $config_file = "include/mbedtls/config.h"; + $include_dir = "include"; + $library_dir = "library"; +} else { + # library directory is not present, we are using Mbed OS namespace + $config_file = "inc/mbedtls/config.h"; + $include_dir = "inc"; + $library_dir = "src"; +} + +if ( length $mbedtls_config_file ) { + $mbedtls_config_file = substr $mbedtls_config_file, 1, -1; + $config_file = $include_dir . "/" . $mbedtls_config_file; +} + my $usage = < | --file ] [-o | --force] [set | unset | get | @@ -205,7 +226,7 @@ if (! -f $config_file) { chdir '..' or die; # Confirm this is the project root directory and try again - if ( !(-d 'scripts' && -d 'include' && -d 'library' && -f $config_file) ) { + if ( !(-d 'scripts' && -d $include_dir && -d $library_dir && -f $config_file) ) { die "If no file specified, must be run from the project root or scripts directory.\n"; } } diff --git a/tests/Makefile b/tests/Makefile index 20a3fe4b7..bc3aba55e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -6,8 +6,23 @@ CFLAGS ?= -O2 WARNING_CFLAGS ?= -Wall -W -Wdeclaration-after-statement -Wunused LDFLAGS ?= -LOCAL_CFLAGS = $(WARNING_CFLAGS) -I../include -D_FILE_OFFSET_BITS=64 -LOCAL_LDFLAGS = -L../library \ +# Check test environment. If ../library is available then Mbed TLS is used. +# Otherwise Mbed OS environment is used. +DIR_FOR_MBED_TLS_ENV=../library +ifneq "$(wildcard $(DIR_FOR_MBED_TLS_ENV) )" "" + LIBRARY_DIR=../library + INCLUDE_DIR=-I../include +else + LIBRARY_DIR=../src + INCLUDE_DIR=-I.. -I../inc +endif + +LOCAL_CFLAGS = $(WARNING_CFLAGS) $(INCLUDE_DIR) -D_FILE_OFFSET_BITS=64 +ifdef MBEDTLS_CONFIG_FILE +LOCAL_CFLAGS += "-DMBEDTLS_CONFIG_FILE=\"${MBEDTLS_CONFIG_FILE}\"" +endif + +LOCAL_LDFLAGS = -L$(LIBRARY_DIR) \ -lmbedtls$(SHARED_SUFFIX) \ -lmbedx509$(SHARED_SUFFIX) \ -lmbedcrypto$(SHARED_SUFFIX) @@ -18,9 +33,9 @@ LOCAL_LDFLAGS = -L../library \ LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L ifndef SHARED -DEP=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a +DEP=$(LIBRARY_DIR)/libmbedcrypto.a $(LIBRARY_DIR)/libmbedx509.a $(LIBRARY_DIR)/libmbedtls.a else -DEP=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT) +DEP=$(LIBRARY_DIR)/libmbedcrypto.$(DLEXT) $(LIBRARY_DIR)/libmbedx509.$(DLEXT) $(LIBRARY_DIR)/libmbedtls.$(DLEXT) endif ifdef DEBUG @@ -77,7 +92,7 @@ BINARIES := $(addsuffix $(EXEXT),$(APPS)) all: $(BINARIES) $(DEP): - $(MAKE) -C ../library + $(MAKE) -C $(LIBRARY_DIR) $(INCLUDE_DIR) C_FILES := $(addsuffix .c,$(APPS))