From ff58e6506dafd1220bd0d908545a4a1e75cf2b09 Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Sat, 4 Mar 2023 16:39:16 +0100 Subject: [PATCH] Fix CTRL-C one time use on Windows --- config.mk | 2 +- musique/main.cc | 16 ++++++++++++++-- scripts/build.mk | 8 ++++---- scripts/build_replxx.sh | 10 +++++----- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/config.mk b/config.mk index f616dda..977612d 100644 --- a/config.mk +++ b/config.mk @@ -13,7 +13,7 @@ VERSION := $(MAJOR).$(MINOR).$(PATCH)-dev+$(COMMIT) CXXFLAGS:=$(CXXFLAGS) -std=c++20 -Wall -Wextra -Werror=switch -Werror=return-type -Werror=unused-result -ggdb CPPFLAGS:=$(CPPFLAGS) -DMusique_Version='"$(VERSION)"' \ - -Ilib/expected/ -I. -Ilib/bestline/ -Ilib/rtmidi/ -Ilib/link/include -Ilib/asio/include/ -Ilib/replxx/include + -Ilib/expected/ -I. -Ilib/bestline/ -Ilib/rtmidi/ -Ilib/link/include -Ilib/asio/include/ -Ilib/replxx/include -DREPLXX_STATIC LDFLAGS=-flto LDLIBS= -lpthread diff --git a/musique/main.cc b/musique/main.cc index 994f1e7..b15e67a 100644 --- a/musique/main.cc +++ b/musique/main.cc @@ -225,6 +225,7 @@ struct Runner } } catch (KeyboardInterrupt const&) { interpreter.turn_off_all_active_notes(); + interpreter.starter.stop(); std::cout << std::endl; } return {}; @@ -357,6 +358,16 @@ static Result handle_repl_session_commands(std::string_view input, Runner return false; } +static Runner *runner; + +void sigint_handler(int sig) +{ + if (sig == SIGINT) { + runner->interpreter.issue_interrupt(); + } + std::signal(SIGINT, sigint_handler); +} + /// Fancy main that supports Result forwarding on error (Try macro) static std::optional Main(std::span args) { @@ -431,8 +442,9 @@ static std::optional Main(std::span args) std::exit(1); } - static Runner runner; - std::signal(SIGINT, [](int sig) { if (sig == SIGINT) runner.interpreter.issue_interrupt(); }); + Runner runner; + ::runner = &runner; + std::signal(SIGINT, sigint_handler); for (auto const& [type, argument] : runnables) { if (type == Run::Argument) { diff --git a/scripts/build.mk b/scripts/build.mk index cac7bdc..b587c58 100755 --- a/scripts/build.mk +++ b/scripts/build.mk @@ -1,15 +1,15 @@ Release_Obj=$(addprefix bin/$(os)/,$(Obj)) -bin/$(os)/libreplxx.a: - @CXX=$(CXX) os=$(os) scripts/build_replxx.sh +# bin/$(os)/libreplxx.a: +# @CXX=$(CXX) os=$(os) scripts/build_replxx.sh bin/$(os)/%.o: musique/%.cc @echo "CXX $@" @$(CXX) $(CXXFLAGS) $(RELEASE_FLAGS) $(CPPFLAGS) -o $@ $< -c -bin/$(os)/$(Target): $(Release_Obj) bin/$(os)/main.o bin/$(os)/rtmidi.o bin/$(os)/libreplxx.a +bin/$(os)/$(Target): $(Release_Obj) bin/$(os)/main.o bin/$(os)/rtmidi.o @echo "CXX $@" - @$(CXX) $(CXXFLAGS) $(RELEASE_FLAGS) $(CPPFLAGS) -o $@ $(Release_Obj) bin/$(os)/rtmidi.o -Lbin/$(os)/ $(LDFLAGS) $(LDLIBS) -lreplxx + @$(CXX) $(CXXFLAGS) $(RELEASE_FLAGS) $(CPPFLAGS) -o $@ $(shell CXX=$(CXX) os=$(os) scripts/build_replxx.sh) $(Release_Obj) bin/$(os)/rtmidi.o $(LDFLAGS) $(LDLIBS) Debug_Obj=$(addprefix bin/$(os)/debug/,$(Obj)) diff --git a/scripts/build_replxx.sh b/scripts/build_replxx.sh index 3a890ea..cacdd9b 100755 --- a/scripts/build_replxx.sh +++ b/scripts/build_replxx.sh @@ -8,13 +8,13 @@ mkdir -p "bin/${os}/replxx" objects="" -echo "Building libreplxx library archive" - -ar rcs "bin/${os}/libreplxx.a" $(find lib/replxx/src/ -name '*.cxx' -o -name '*.cpp' | while read -r src +find lib/replxx/src/ -name '*.cxx' -o -name '*.cpp' | while read -r src do dst="${src##"$prefix"}" dst="bin/${os}/replxx/${dst%%.*}.o" - "${CXX}" -Ilib/replxx/src/ -Ilib/replxx/include/ -c -o "$dst" "$src" -std=c++20 -O3 -DREPLXX_STATIC + if [ ! -f "$dst" ]; then + "${CXX}" -Ilib/replxx/src/ -Ilib/replxx/include/ -c -o "$dst" "$src" -std=c++20 -O3 -DREPLXX_STATIC + fi echo "${dst}" -done) +done