From 3ad566cc931589220e901862f48bcb5af32073b7 Mon Sep 17 00:00:00 2001 From: Borislav Demidov Date: Sun, 23 Jul 2023 01:09:30 +0300 Subject: [PATCH] usb: make usb optional at buildtime USB was enabled by default on Linux, and disabled for other OS. Lets make it cmake option (NM_WITH_USB). That way we can build nemu on Linux without requiring udev/libusb. Closes #162 --- .copr/Makefile | 3 ++- .github/workflows/linux.yml | 4 ++-- .github/workflows/linux_musl.yml | 2 +- .github/workflows/tests.yml | 2 +- CMakeLists.txt | 21 ++++++++++++++------- README_Build.md | 2 +- lang/update.sh | 3 ++- pkg/linux/debian/rules | 3 ++- pkg/linux/gentoo/nemu-9999.ebuild | 11 +++++++---- pkg/linux/nixos/package.nix | 9 +++++---- pkg/linux/rpm/CMakeLists.txt | 2 ++ src/nm_add_vm.c | 4 ++-- src/nm_main.c | 7 +++++++ src/nm_main_loop.c | 4 +++- src/nm_usb_devices.c | 16 ++++++++-------- src/nm_window.c | 6 +++--- 16 files changed, 62 insertions(+), 37 deletions(-) diff --git a/.copr/Makefile b/.copr/Makefile index 072e7b17..75d3fb14 100644 --- a/.copr/Makefile +++ b/.copr/Makefile @@ -28,7 +28,8 @@ build: dirs -DNM_WITH_NETWORK_MAP=ON \ -DNM_WITH_DBUS=ON \ -DNM_WITH_REMOTE=ON \ - -DNM_WITH_QEMU=ON && \ + -DNM_WITH_QEMU=ON \ + -DNM_WITH_USB=ON && \ cpack -G RPM --config ./CPackSourceConfig.cmake && \ cp *.src.rpm $(outdir) && \ cd ${TOP_DIR} && \ diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 2f365836..46f17302 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -30,7 +30,7 @@ jobs: - name: Configure nEMU build run: | cmake -G Ninja \ - -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON + -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON -DNM_WITH_USB=ON - name: Build nEMU run: cmake --build . @@ -64,5 +64,5 @@ jobs: rm CMakeCache.txt mkdir build && cd build cmake -G Ninja .. \ - -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON + -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON -DNM_WITH_USB=ON cmake --build . diff --git a/.github/workflows/linux_musl.yml b/.github/workflows/linux_musl.yml index d125805e..55ca0eaf 100644 --- a/.github/workflows/linux_musl.yml +++ b/.github/workflows/linux_musl.yml @@ -22,7 +22,7 @@ jobs: - name: Configure nEMU build run: | cmake -G Ninja \ - -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON + -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON -DNM_WITH_USB=ON - name: Build nEMU run: cmake --build . diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 06622778..1764252a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -32,7 +32,7 @@ jobs: - name: Build nEMU run: | cmake -G Ninja \ - -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON + -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON -DNM_WITH_USB=ON cmake --build . - name: Cache testdata diff --git a/CMakeLists.txt b/CMakeLists.txt index 6033f6a0..2c86d337 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,7 @@ option(NM_WITH_DBUS "Build with D-Bus support" OFF) option(NM_WITH_QEMU "Build with embedded QEMU" OFF) option(NM_WITH_NCURSES "Build with embedded statically linked ncurses" OFF) option(NM_WITH_REMOTE "Build with remote control" OFF) +option(NM_WITH_USB "Build with USB support" OFF) include_directories(src) aux_source_directory(src SRC_LIST) @@ -106,8 +107,6 @@ find_package(Sqlite3 REQUIRED) find_package(Threads REQUIRED) if(NOT NM_OS_DARWIN) find_package(RT REQUIRED) -find_package(libusb-1.0 REQUIRED) -find_package(UDev REQUIRED) endif() find_package(Intl REQUIRED) pkg_check_modules(JSONC REQUIRED json-c) @@ -115,16 +114,14 @@ pkg_check_modules(JSONC REQUIRED json-c) if(NOT NM_OS_DARWIN) target_link_libraries( ${PROJECT_NAME} ${CURSES_LIBRARIES} ${SQLITE3_LIBRARIES} ${JSONC_LINK_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} ${LIBUSB_1_LIBRARIES} ${UDEV_LIBRARIES} - ${RT_LIBRARY} ${Intl_LIBRARIES}) + ${CMAKE_THREAD_LIBS_INIT} ${RT_LIBRARY} ${Intl_LIBRARIES}) else() target_link_libraries( ${PROJECT_NAME} ${CURSES_LIBRARIES} ${SQLITE3_LIBRARIES} ${JSONC_LINK_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} ${LIBUSB_1_LIBRARIES} ${Intl_LIBRARIES}) + ${CMAKE_THREAD_LIBS_INIT} ${Intl_LIBRARIES}) endif() include_directories(${CURSES_INCLUDE_PATH} ${SQLITE3_INCLUDE_DIR} - ${UDEV_INCLUDE_DIR} ${JSONC_INCLUDE_DIRS} ${Intl_INCLUDE_DIRS}) -include_directories(SYSTEM ${LIBUSB_1_INCLUDE_DIRS}) + ${JSONC_INCLUDE_DIRS} ${Intl_INCLUDE_DIRS}) add_definitions(-DNM_FULL_DATAROOTDIR="${CMAKE_INSTALL_FULL_DATAROOTDIR}") add_definitions(-DNM_FULL_LOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}") @@ -243,6 +240,15 @@ if(NM_WITH_QEMU) add_definitions(-DNM_WITH_QEMU) endif() +if(NM_WITH_USB) + find_package(libusb-1.0 REQUIRED) + find_package(UDev REQUIRED) + target_link_libraries(${PROJECT_NAME} ${LIBUSB_1_LIBRARIES} ${UDEV_LIBRARIES}) + include_directories(${UDEV_INCLUDE_DIR}) + include_directories(SYSTEM ${LIBUSB_1_INCLUDE_DIRS}) + add_definitions(-DNM_WITH_USB) +endif() + if(NOT GIT_TAG_VERSION) execute_process( COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/sh/git_get_version.sh @@ -357,6 +363,7 @@ message(STATUS "OVF support: ${NM_WITH_OVF_SUPPORT}") message(STATUS "Network map export: ${NM_WITH_NETWORK_MAP}") message(STATUS "D-Bus support: ${NM_WITH_DBUS}") message(STATUS "Remote control support: ${NM_WITH_REMOTE}") +message(STATUS "USB support: ${NM_WITH_USB}") message(STATUS "Embedded QEMU: ${NM_WITH_QEMU}") if(NM_WITH_QEMU) message(STATUS "Embedded QEMU targets: ${NM_QEMU_TARGET_LIST}") diff --git a/README_Build.md b/README_Build.md index c8de7122..8c281f06 100644 --- a/README_Build.md +++ b/README_Build.md @@ -9,7 +9,7 @@ $ git clone https://github.com/nemuTUI/nemu ```sh $ cd nemu $ mkdir build && cd build -$ cmake -G Ninja .. -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON +$ cmake -G Ninja .. -DNM_WITH_NETWORK_MAP=ON -DNM_WITH_DBUS=ON -DNM_WITH_REMOTE=ON -DNM_WITH_USB=ON $ cmake --build . ``` diff --git a/lang/update.sh b/lang/update.sh index 60d8e58b..0c426e9f 100755 --- a/lang/update.sh +++ b/lang/update.sh @@ -22,6 +22,7 @@ for F in $FILES; do -DNM_WITH_DBUS -DNM_WITH_NETWORK_MAP \ -DNM_WITH_OVF_SUPPORT -DNM_WITH_REMOTE \ -DNM_WITH_SPICE -DNM_WITH_VNC_CLIENT \ + -DNM_WITH_USB \ -E $F -o ${TMPDIR}/${E}.e done @@ -32,7 +33,7 @@ pushd $TMPDIR > /dev/null # static const char msg[] = "message"; # printf("%s", _(msg)); # -# so we have to edit code. We use NM_LC_ prefix for strings +# so we have to edit code. We use NM_LC_ prefix for strings # that must be processed by gettext. for F in $FILES; do E=${F//\.c/\.e} diff --git a/pkg/linux/debian/rules b/pkg/linux/debian/rules index 2beb58e8..c936ea74 100755 --- a/pkg/linux/debian/rules +++ b/pkg/linux/debian/rules @@ -9,4 +9,5 @@ DEB_CMAKE_EXTRA_FLAGS := \ -DNM_WITH_OVF_SUPPORT=ON \ -DNM_WITH_REMOTE=ON \ -DNM_WITH_SPICE=ON \ - -DNM_WITH_VNC_CLIENT=ON + -DNM_WITH_VNC_CLIENT=ON \ + -DNM_WITH_USB=ON diff --git a/pkg/linux/gentoo/nemu-9999.ebuild b/pkg/linux/gentoo/nemu-9999.ebuild index 1487f3c7..69e5d2da 100644 --- a/pkg/linux/gentoo/nemu-9999.ebuild +++ b/pkg/linux/gentoo/nemu-9999.ebuild @@ -13,14 +13,12 @@ SRC_URI="" LICENSE="BSD-2" SLOT="0" -IUSE="+ovf dbus network-map remote-api" +IUSE="+ovf dbus network-map remote-api +usb" RDEPEND=" dev-libs/json-c sys-libs/ncurses dev-db/sqlite:3= - dev-libs/libusb:1= - || ( sys-fs/eudev sys-fs/udev sys-apps/systemd ) >=app-emulation/qemu-6.0.0-r2[vnc,virtfs,spice] ovf? ( dev-libs/libxml2 @@ -28,7 +26,11 @@ RDEPEND=" ) dbus? ( sys-apps/dbus ) remote-api? ( dev-libs/openssl ) - network-map? ( media-gfx/graphviz[svg] )" + network-map? ( media-gfx/graphviz[svg] ) + usb? ( + dev-libs/libusb:1 + || ( sys-fs/eudev sys-fs/udev sys-apps/systemd ) + )" DEPEND="${RDEPEND} sys-devel/gettext" @@ -38,6 +40,7 @@ src_configure() { -DNM_WITH_NETWORK_MAP=$(usex network-map) -DNM_WITH_REMOTE=$(usex remote-api) -DNM_WITH_DBUS=$(usex dbus) + -DNM_WITH_USB=$(usex usb) -DCMAKE_INSTALL_PREFIX=/usr ) cmake_src_configure diff --git a/pkg/linux/nixos/package.nix b/pkg/linux/nixos/package.nix index 685a7945..f6902303 100644 --- a/pkg/linux/nixos/package.nix +++ b/pkg/linux/nixos/package.nix @@ -26,6 +26,7 @@ , withSpice ? true , withVNC ? true , withRemote ? false +, withUSB ? true , vmDir ? ".local/share/nemu/vms" , databaseName ? ".local/share/nemu/nemu.db" @@ -52,8 +53,6 @@ stdenv.mkDerivation rec { buildInputs = [ gettext libpthreadstubs - libudev - libusb1 sqlite qemu ncurses @@ -66,7 +65,8 @@ stdenv.mkDerivation rec { ++ lib.optionals withOVF [ libxml2 libarchive ] ++ lib.optional withSpice virtviewer ++ lib.optional withVNC tigervnc - ++ lib.optional withRemote openssl; + ++ lib.optional withRemote openssl + ++ lib.optional withUSB [ libudev libusb1 ]; cmakeFlags = [ "-DNM_DEFAULT_VMDIR=${vmDir}" @@ -80,7 +80,8 @@ stdenv.mkDerivation rec { ++ lib.optional withOVF "-DNM_WITH_OVF_SUPPORT=ON" ++ lib.optional withSpice "-DNM_WITH_SPICE=ON" ++ lib.optional withVNC "-DNM_WITH_VNC_CLIENT=ON" - ++ lib.optional withRemote "-DNM_WITH_REMOTE=ON"; + ++ lib.optional withRemote "-DNM_WITH_REMOTE=ON" + ++ lib.optional withUSB "-DNM_WITH_USB=ON"; preConfigure = '' patchShebangs . diff --git a/pkg/linux/rpm/CMakeLists.txt b/pkg/linux/rpm/CMakeLists.txt index 89c71e38..3d9ec69e 100644 --- a/pkg/linux/rpm/CMakeLists.txt +++ b/pkg/linux/rpm/CMakeLists.txt @@ -50,6 +50,8 @@ if(CPACK_RPM_PACKAGE_SOURCES) ) set(CPACK_RPM_SOURCE_PKG_BUILD_PARAMS "${CPACK_RPM_SOURCE_PKG_BUILD_PARAMS} -DNM_WITH_NCURSES=${NM_WITH_NCURSES}") + set(CPACK_RPM_SOURCE_PKG_BUILD_PARAMS + "${CPACK_RPM_SOURCE_PKG_BUILD_PARAMS} -DNM_WITH_USB=${NM_WITH_USB}") elseif(NM_WITH_QEMU) set(CPACK_RPM_PACKAGE_REQUIRES "bzip2") diff --git a/src/nm_add_vm.c b/src/nm_add_vm.c index f8a7ac37..b3884eef 100644 --- a/src/nm_add_vm.c +++ b/src/nm_add_vm.c @@ -349,9 +349,9 @@ static int nm_add_vm_get_data(nm_vm_t *vm) } vm->ifs.count = nm_str_stoui(&ifs_buf, 10); -#if defined (NM_OS_LINUX) +#if defined (NM_WITH_USB) vm->usb_enable = 1; /* enable USB by default */ -#endif +#endif /* NM_WITH_USB */ rc = nm_form_name_used(&vm->name); diff --git a/src/nm_main.c b/src/nm_main.c index 891e125b..7e3d4793 100644 --- a/src/nm_main.c +++ b/src/nm_main.c @@ -410,6 +410,13 @@ static void nm_print_feset(void) NM_FESET(" Remote control", YES, "+") #else NM_FESET(" Remote control", NO, "-") +#endif + ); + nm_vect_insert_cstr(&feset, +#if defined (NM_WITH_USB) + NM_FESET(" USB Support", YES, "+") +#else + NM_FESET(" USB Support", NO, "-") #endif ); diff --git a/src/nm_main_loop.c b/src/nm_main_loop.c index 3f16a0ca..67f70cf4 100644 --- a/src/nm_main_loop.c +++ b/src/nm_main_loop.c @@ -292,7 +292,7 @@ void nm_start_main_loop(void) nm_init_side(); break; -#if defined(NM_OS_LINUX) +#if defined(NM_WITH_USB) case NM_KEY_PLUS: nm_usb_plug(name, vm_status); break; @@ -300,7 +300,9 @@ void nm_start_main_loop(void) case NM_KEY_MINUS: nm_usb_unplug(name, vm_status); break; +#endif /* NM_WITH_USB */ +#if defined(NM_OS_LINUX) case NM_KEY_H: nm_9p_share(name); break; diff --git a/src/nm_usb_devices.c b/src/nm_usb_devices.c index a32d1a18..5af6135a 100644 --- a/src/nm_usb_devices.c +++ b/src/nm_usb_devices.c @@ -11,7 +11,7 @@ enum {NM_USB_SERIAL_LEN = 127}; static inline void nm_usb_dev_free(nm_usb_dev_t *dev); -#if defined (NM_OS_LINUX) +#if defined (NM_WITH_USB) #include #include @@ -23,11 +23,11 @@ static int nm_usb_get_product_str(char *buf, size_t size, uint16_t vid, uint16_t pid); static struct udev_hwdb *hwdb; -#endif /* NM_OS_LINUX */ +#endif /* NM_WITH_USB */ void nm_usb_get_devs(nm_vect_t *v) { -#if defined(NM_OS_LINUX) +#if defined(NM_WITH_USB) libusb_device **list = NULL; libusb_context *ctx = NULL; struct udev *udev = NULL; @@ -93,13 +93,13 @@ void nm_usb_get_devs(nm_vect_t *v) libusb_exit(ctx); #else (void) v; -#endif /* NM_OS_LINUX */ +#endif /* NM_WITH_USB */ } int nm_usb_get_serial(const nm_usb_dev_t *dev, nm_str_t *serial) { int rc = NM_ERR; -#if defined (NM_OS_LINUX) +#if defined (NM_WITH_USB) libusb_context *ctx = NULL; libusb_device **list = NULL; int usb_rc; @@ -158,7 +158,7 @@ int nm_usb_get_serial(const nm_usb_dev_t *dev, nm_str_t *serial) #else (void) dev; (void) serial; -#endif /* NM_OS_LINUX */ +#endif /* NM_WITH_USB */ return rc; } @@ -203,7 +203,7 @@ static inline void nm_usb_dev_free(nm_usb_dev_t *dev) nm_str_free(&dev->product_id); } -#if defined (NM_OS_LINUX) +#if defined (NM_WITH_USB) static const char *nm_usb_hwdb_get(const char *modalias, const char *key) { struct udev_list_entry *entry = NULL; @@ -268,6 +268,6 @@ nm_usb_get_product_str(char *buf, size_t size, uint16_t vid, uint16_t pid) return snprintf(buf, size, "%s", cp); } -#endif /* NM_OS_LINUX */ +#endif /* NM_WITH_USB */ /* vim:set ts=4 sw=4: */ diff --git a/src/nm_window.c b/src/nm_window.c index dac38187..4fde874a 100644 --- a/src/nm_window.c +++ b/src/nm_window.c @@ -703,7 +703,7 @@ void nm_print_help(void) "i", "C", "a", "l", "b", "h", "m", "v", "u", "P", "R", "S", "X", "D", -#if defined (NM_OS_LINUX) +#if defined (NM_WITH_USB) "+", "-", #endif "k", "/", "[", "]" @@ -733,10 +733,10 @@ void nm_print_help(void) "take vm snapshot", "revert vm snapshot", "delete vm snapshot", -#if defined (NM_OS_LINUX) +#if defined(NM_WITH_USB) "attach usb device", "detach usb device", -#endif +#endif /* NM_WITH_USB */ "kill vm process", "search vm, filters", "previous group",