From aa1d8d1def6119dd22be631e54cfae81d42f150d Mon Sep 17 00:00:00 2001 From: Steven Van Ingelgem Date: Thu, 14 May 2020 06:23:26 +0200 Subject: [PATCH] Initial release. --- .gitignore | 8 ++++++ CMakeLists.txt | 42 +++++++++++++++++++++++++++++ build_all.cmd | 14 ++++++++++ builder.cmd | 55 ++++++++++++++++++++++++++++++++++++++ ptime.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 190 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 build_all.cmd create mode 100644 builder.cmd create mode 100644 ptime.cpp diff --git a/.gitignore b/.gitignore index 259148f..46cbc00 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,11 @@ *.exe *.out *.app +/build/ +/build_32/ +/build_64/ +/_CPack_Packages/ + + +*.zip +*.exe diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..322e7fa --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,42 @@ +cmake_minimum_required(VERSION 3.0) + + +project(ptime) + + +add_executable( + ${PROJECT_NAME} + + ptime.cpp +) + + +target_link_libraries( + ${PROJECT_NAME} + Winmm +) + + +install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION .) + + +find_package(SelfPackers) +if(SELF_PACKER_FOR_EXECUTABLE) + set(release $) + add_custom_command( + TARGET ${PROJECT_NAME} + POST_BUILD + COMMAND "$<${release}:${SELF_PACKER_FOR_EXECUTABLE}>" "$<${release}:-9>" "$<${release}:$>" + ) +endif() + + +set(CPACK_PACKAGE_NAME "${PROJECT_NAME}") +set(CPACK_PACKAGE_VENDOR "Salvania") +set(CPACK_GENERATOR "ZIP") +set(CPACK_PACKAGE_DIRECTORY "${CMAKE_SOURCE_DIR}") +set(CPACK_VERBATIM_VARIABLES true) +set(CPACK_PACKAGE_VERSION_MAJOR 1) +set(CPACK_PACKAGE_VERSION_MINOR 0) +set(CPACK_PACKAGE_VERSION_PATCH 0) +include(CPack) diff --git a/build_all.cmd b/build_all.cmd new file mode 100644 index 0000000..ad211f5 --- /dev/null +++ b/build_all.cmd @@ -0,0 +1,14 @@ +@echo off + + +set MY_DIR="%cd%" + +cd "%~dp0" + +call builder -32bit -Release -upx +call builder -64bit -Release -upx + +cd %MY_DIR% + + +@echo on diff --git a/builder.cmd b/builder.cmd new file mode 100644 index 0000000..1999880 --- /dev/null +++ b/builder.cmd @@ -0,0 +1,55 @@ +@echo off + + +rem https://stackoverflow.com/questions/3785976/cmake-generate-visual-studio-2008-solution-for-win32-and-x64 + + +rem ===== Usage +rem builder [-32bit|-64bit] [-Debug|-Release] +rem --> Defaults: -64bit -Release + + +rem Global configuration variables. Should be update based on your system +SET VISUAL_STUDIO_HOME=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC +SET VISUAL_STUDIO_NAME=Visual Studio 16 2019 +SET CMAKE_HOME=C:\Program Files\CMake + +rem First parameter is project folder path +SET PROJECT_DIR="%~dp0" +SET ORIGINAL_DIR="%cd%" +SET CMAKE="%CMAKE_HOME%\bin\cmake.exe" + +rem Go to project directory +cd %PROJECT_DIR% + +rem Second parameter defines 32 bit or 64 bit compilation +if "%1"=="-32bit" ( + echo === Generating 32bit project === + + rmdir /s /q build_32 + md build_32 + cd build_32 + %CMAKE% .. -G "%VISUAL_STUDIO_NAME%" -A Win32 +) ELSE ( + echo === Generating 64bit project === + + rmdir /s /q build_64 + md build_64 + cd build_64 + %CMAKE% .. -G "%VISUAL_STUDIO_NAME%" -A x64 +) + +rem Third parameter defines debug or release compilation +if "%2"=="-Debug" ( + echo === Building in Debug mode === + %CMAKE% --build . --target PACKAGE --config Debug +) ELSE ( + echo === Building in Release mode === + %CMAKE% --build . --target PACKAGE --config Release +) + + +rem Go to source code directory and finalize script +cd %ORIGINAL_DIR% + +@echo on diff --git a/ptime.cpp b/ptime.cpp new file mode 100644 index 0000000..f209453 --- /dev/null +++ b/ptime.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + +// https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string +std::string ReplaceString(std::string subject, const std::string& search, + const std::string& replace) { + size_t pos = 0; + while ((pos = subject.find(search, pos)) != std::string::npos) { + subject.replace(pos, search.length( ), replace); + pos += replace.length( ); + } + return subject; +} + + +string escape_shell_argument(string in) { + static const string single_quote = "\""; + static const string escaped_single_quote = "\\\""; + + string quoted = ReplaceString(in, single_quote, escaped_single_quote); + if (count_if(quoted.begin( ), quoted.end( ), [] (unsigned char c) { return std::isspace(c); }) > 0) { + return single_quote + quoted + single_quote; + } + else { + return quoted; + } +} + + +void print_help( ) { + cerr << "Syntax: ptime command [arguments ...]" << endl + << endl + << "ptime will run the specified command and measure the execution time" << endl + << "(run time) in seconds, accurate to 5 millisecond or better. It is an" << endl + << "automatic process timer, or program timer." << endl; +} + + +int main(int argc, const char* argv[]) { + if (argc <= 1) { + print_help( ); + return -1; + } + + std::stringstream cmd_to_run; + for (int i = 1; i < argc; i++) { + if (i != 1) cmd_to_run << ' '; + cmd_to_run << escape_shell_argument(argv[i]); + } + + + cout << "=== " << cmd_to_run.str() << " ===" << endl; + + timeBeginPeriod(1u); + DWORD start_time = timeGetTime( ); + system(cmd_to_run.str().c_str()); + DWORD total_time = timeGetTime( ) - start_time; + timeEndPeriod(1u); + + cout << "=== Execution time: " << std::fixed << std::setprecision(3) << (total_time * 0.001) << " s ===" << endl; + + return 0; +}