From f9eba5b179d3f80da72211f4c7a48c78aed2bef7 Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Wed, 1 Jul 2020 01:14:03 +0800 Subject: [PATCH] Update build scripts Optimize codes. --- CMakeLists.txt | 29 ++++++----------------------- scripts/build.alpine.release.sh | 19 +++++++++++++------ scripts/build.macos.release.sh | 22 +++++++++------------- src/misc.h | 6 +++--- src/subexport.cpp | 2 +- 5 files changed, 32 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d087e66..0db1495 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,8 @@ ENDIF() #remove std::regex support since it is not compatible with group modifiers and slow #OPTION(USING_STD_REGEX "Use std::regex from C++ library instead of PCRE2." OFF) OPTION(USING_MALLOC_TRIM "Call malloc_trim after processing request to lower memory usage (Your system must support malloc_trim)." OFF) -OPTION(USING_MBEDTLS "Use mbedTLS instead of OpenSSL for MD5 calculation." OFF) +#now using internal MD5 calculation +#OPTION(USING_MBEDTLS "Use mbedTLS instead of OpenSSL for MD5 calculation." OFF) INCLUDE(CheckCXXSourceCompiles) CHECK_CXX_SOURCE_COMPILES( @@ -26,10 +27,6 @@ CHECK_CXX_SOURCE_COMPILES( void main(){std::to_string(0);} " HAVE_TO_STRING) -IF(APPLE) - ADD_DEFINITIONS(-D_MACOS) -ENDIF() - IF(HAVE_TO_STRING) ADD_DEFINITIONS(-DHAVE_TO_STRING) ENDIF() @@ -62,34 +59,20 @@ SET(THREADS_PREFER_PTHREAD_FLAG ON) FIND_PACKAGE(Threads REQUIRED) TARGET_LINK_LIBRARIES(subconverter ${CMAKE_THREAD_LIBS_INIT}) -PKG_CHECK_MODULES(LIBEVENT libevent REQUIRED) -FIND_PATH(LIBEVENT_INCLUDE_DIR NAMES event.h PATHS ${LIBEVENT_INCLUDE_DIRS}) -FIND_LIBRARY(LIBEVENT_LIBRARY NAMES event PATHS ${LIBEVENT_LIBRARY_DIRS}) +PKG_CHECK_MODULES(LIBEVENT libevent>=2.1.10 REQUIRED) LINK_DIRECTORIES(${LIBEVENT_LIBRARY_DIRS}) INCLUDE_DIRECTORIES(${LIBEVENT_INCLUDE_DIR}) -TARGET_LINK_LIBRARIES(subconverter ${LIBEVENT_LIBRARY}) +TARGET_LINK_LIBRARIES(subconverter ${LIBEVENT_LIBRARIES}) -FIND_PACKAGE(CURL 7.54.0 REQUIRED) +PKG_CHECK_MODULES(CURL libcurl>=7.54.0 REQUIRED) INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS}) TARGET_LINK_LIBRARIES(subconverter ${CURL_LIBRARIES}) ADD_DEFINITIONS(-DCURL_STATICLIB) -IF(USING_MBEDTLS STREQUAL "ON") - FIND_PACKAGE(MbedTLS REQUIRED) - INCLUDE_DIRECTORIES(${MBEDTLS_INCLUDE_DIRS}) - TARGET_LINK_LIBRARIES(subconverter ${MBEDCRYPTO_LIBRARY}) - ADD_DEFINITIONS(-DUSE_MBEDTLS) -ELSE() - FIND_PACKAGE(OpenSSL 1.1.0 REQUIRED) - INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR}) - TARGET_LINK_LIBRARIES(subconverter ${OPENSSL_LIBRARIES}) -ENDIF() - FIND_PACKAGE(Rapidjson REQUIRED) INCLUDE_DIRECTORIES(${RAPIDJSON_INCLUDE_DIRS}) -PKG_CHECK_MODULES(YAML_CPP yaml-cpp REQUIRED) -FIND_LIBRARY(YAML_CPP_LIBRARY NAMES yaml-cpp yaml-cppd PATHS ${YAML_CPP_LIBRARY_DIRS}) +PKG_CHECK_MODULES(YAML_CPP yaml-cpp>=0.6.3 REQUIRED) LINK_DIRECTORIES(${YAML_CPP_LIBRARY_DIRS}) INCLUDE_DIRECTORIES(${YAML_CPP_INCLUDE_DIRS}) TARGET_LINK_LIBRARIES(subconverter ${YAML_CPP_LIBRARY}) diff --git a/scripts/build.alpine.release.sh b/scripts/build.alpine.release.sh index 7cbf3a7..0667b15 100644 --- a/scripts/build.alpine.release.sh +++ b/scripts/build.alpine.release.sh @@ -2,7 +2,13 @@ set -xe apk add gcc g++ build-base linux-headers cmake make autoconf automake libtool python2 -apk add openssl-dev openssl-libs-static curl curl-dev curl-static nghttp2-static zlib-dev rapidjson-dev libevent-dev libevent-static zlib-static pcre2-dev bzip2-static +apk add mbedtls-dev mbedtls-static nghttp2-dev nghttp2-static zlib-dev rapidjson-dev libevent-dev libevent-static zlib-static pcre2-dev + +git clone https://github.com/curl/curl --depth=1 +cd curl +cmake -DCMAKE_USE_MBEDTLS=ON -DHTTP_ONLY=ON -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_USE_LIBSSH2=OFF -DUSE_NGHTTP2=ON . > /dev/null +make install -j2 > /dev/null +cd .. git clone https://github.com/jbeder/yaml-cpp --depth=1 cd yaml-cpp @@ -14,21 +20,22 @@ git clone https://github.com/svaarala/duktape --depth=1 cd duktape python2 -m ensurepip pip2 install PyYAML +mkdir dist python2 util/dist.py -cd dist/src +cd dist/source/src cc -c -O3 -o duktape.o duktape.c cc -c -O3 -o duk_module_node.o -I. ../extras/module-node/duk_module_node.c ar cr libduktape.a duktape.o ar cr libduktape_module.a duk_module_node.o -install -m0644 *.a /usr/lib -install -m0644 duk*.h /usr/include +install -m0644 ./*.a /usr/lib +install -m0644 ./duk*.h /usr/include install -m0644 ../extras/module-node/duk_module_node.h /usr/include -cd ../../.. +cd ../../../.. cmake . make -j2 rm subconverter -g++ -o base/subconverter CMakeFiles/subconverter.dir/src/*.o -static -lpcre2-8 -levent -lyaml-cpp -lcurl -lnghttp2 -lssl -lcrypto -lz -lbz2 -lduktape -lduktape_module -ldl -lpthread -O3 -s +g++ -o base/subconverter $(find CMakeFiles/subconverter.dir/src/ -name "*.o") -static -lpcre2-8 -levent -lyaml-cpp -L/usr/lib64 -lcurl -lnghttp2 -lmbedtls -lmbedcrypto -lmbedx509 -lz -lduktape -lduktape_module -O3 -s cd base chmod +rx subconverter diff --git a/scripts/build.macos.release.sh b/scripts/build.macos.release.sh index af389ab..c7c2287 100644 --- a/scripts/build.macos.release.sh +++ b/scripts/build.macos.release.sh @@ -1,13 +1,13 @@ #!/bin/bash set -xe -brew reinstall rapidjson libevent zlib pcre2 bzip2 libssh2 pkgconfig +brew reinstall rapidjson libevent zlib pcre2 pkgconfig git clone https://github.com/curl/curl --depth=1 cd curl #./buildconf > /dev/null #./configure --with-ssl=/usr/local/opt/openssl@1.1 --without-mbedtls --disable-ldap --disable-ldaps --disable-rtsp --without-libidn2 > /dev/null -cmake -DHTTP_ONLY=ON -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=OFF -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl@1.1 . > /dev/null +cmake -DCMAKE_USE_SECTRANSP=ON -DHTTP_ONLY=ON -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_USE_LIBSSH2=OFF . > /dev/null make -j8 > /dev/null cd .. @@ -20,33 +20,29 @@ cd .. git clone https://github.com/svaarala/duktape --depth=1 cd duktape pip2 install PyYAML +mkdir dist python2 util/dist.py -cd dist/src +cd dist/source/src cc -c -O3 -o duktape.o duktape.c cc -c -O3 -o duk_module_node.o -I. ../extras/module-node/duk_module_node.c ar cr libduktape.a duktape.o ar cr libduktape_module.a duk_module_node.o -install -m0644 *.a /usr/local/lib -install -m0644 duk*.h /usr/local/include +install -m0644 ./*.a /usr/local/lib +install -m0644 ./duk*.h /usr/local/include install -m0644 ../extras/module-node/duk_module_node.h /usr/local/include -cd ../../.. +cd ../../../.. cp curl/lib/libcurl.a . cp yaml-cpp/libyaml-cpp.a . cp duktape/dist/src/*.a . cp /usr/local/lib/libevent.a . cp /usr/local/opt/zlib/lib/libz.a . -cp /usr/local/opt/openssl@1.1/lib/libssl.a . -cp /usr/local/opt/openssl@1.1/lib/libcrypto.a . cp /usr/local/lib/libpcre2-8.a . -cp /usr/local/opt/bzip2/lib/libbz2.a . -cp /usr/local/lib/libssh2.a . -export CMAKE_CXX_FLAGS="-I/usr/local/include -I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/curl/include" -cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl@1.1 . +cmake . make -j8 rm subconverter -c++ -Xlinker -unexported_symbol -Xlinker "*" -o base/subconverter CMakeFiles/subconverter.dir/src/*.o libpcre2-8.a libevent.a libcurl.a libz.a libssl.a libcrypto.a libyaml-cpp.a libbz2.a libssh2.a libduktape.a libduktape_module.a -ldl -lpthread -O3 +c++ -Xlinker -unexported_symbol -Xlinker "*" -o base/subconverter $(find CMakeFiles/subconverter.dir/src/ -name "*.o") $(find . -name "*.a") -O3 cd base chmod +rx subconverter diff --git a/src/misc.h b/src/misc.h index 4d4269b..b2a0a3f 100644 --- a/src/misc.h +++ b/src/misc.h @@ -23,10 +23,10 @@ #define DO_CONCAT(a,b) CONCAT(a,b) template class __defer_struct final {private: T fn; bool __cancelled = false; public: __defer_struct(T func) : fn(std::move(func)) {} ~__defer_struct() {if(!__cancelled) fn();} void cancel() {__cancelled = true;} }; //#define defer(x) std::unique_ptr DO_CONCAT(__defer_deleter_,__LINE__) (nullptr, [&](...){x}); -#define defer(x) __defer_struct DO_CONCAT(__defer_deleter,__COUNTER__) ([&](...){x;}); +#define defer(x) __defer_struct DO_CONCAT(__defer_deleter,__LINE__) ([&](...){x;}); -#define getbit(x,n) (((int)x < 1) ? 0 : ((x >> (n - 1)) & 1)) -#define setbit(x,n,v) x ^= (-v ^ x) & (1UL << (n - 1)) +#define GETBIT(x,n) (((int)x < 1) ? 0 : ((x >> (n - 1)) & 1)) +#define SETBIT(x,n,v) x ^= (-v ^ x) & (1UL << (n - 1)) typedef std::string::size_type string_size; typedef std::vector string_array; diff --git a/src/subexport.cpp b/src/subexport.cpp index 5fbb818..0ae450d 100644 --- a/src/subexport.cpp +++ b/src/subexport.cpp @@ -1744,7 +1744,7 @@ std::string netchToSingle(std::vector &nodes, int types, extra_setting std::string protocol, protoparam, obfs, obfsparam; std::string id, aid, transproto, faketype, host, path, quicsecure, quicsecret; std::string proxyStr, allLinks; - bool tlssecure, ss = getbit(types, 1), ssr = getbit(types, 2), vmess = getbit(types, 3), trojan = getbit(types, 4); + bool tlssecure, ss = GETBIT(types, 1), ssr = GETBIT(types, 2), vmess = GETBIT(types, 3), trojan = GETBIT(types, 4); for(nodeInfo &x : nodes) {