From 2100b4ce152e9c70b3ce8760112d5a24ead4e52d Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 3 Apr 2020 21:34:45 -0400 Subject: [PATCH] Allow qpdf to be built on systems without wchar_t (fixes #406) --- ChangeLog | 6 ++++++ README.md | 14 ++++++++++++++ include/qpdf/QUtil.hh | 8 ++++++++ libqpdf/QUtil.cc | 5 ++++- libtests/build.mk | 1 + libtests/main_from_wmain.cc | 34 ++++++++++++++++++++++++++++++++++ libtests/qtest/qutil.test | 18 +++++++++++++++++- libtests/qtest/qutil/qutil.out | 4 ---- libtests/qtest/qutil/wmain.out | 3 +++ libtests/qutil.cc | 13 ------------- manual/qpdf-manual.xml | 10 ++++++++++ 11 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 libtests/main_from_wmain.cc create mode 100644 libtests/qtest/qutil/wmain.out diff --git a/ChangeLog b/ChangeLog index 94109b11..10407aa2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2020-04-03 Jay Berkenbilt + * Allow qpdf to be built on systems without wchar_t. All "normal" + systems have wchar_t because it is part of the C++ standard, but + there are some stripped down environments that don't have it. See + README.md (search for wchar_t) for instructions and a discussion. + Fixes #406. + * Add two extra optional arguments to QPDFPageObjectHelper::placeFormXObject to control whether the placed item is allowed to be shrunk or expanded to fit within or diff --git a/README.md b/README.md index b191c3e6..571c1d1a 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,20 @@ make install Packagers may set DESTDIR, in which case make install will install inside of DESTDIR, as is customary with many packages. For more detailed general information, see the "INSTALL" file in this directory. If you are already accustomed to building and installing software that uses autoconf, there's nothing new for you in the INSTALL file. Note that qpdf uses `autoconf` but not `automake`. We have our own system of Makefiles that allows cross-directory dependencies, doesn't use recursive make, and works better on non-UNIX platforms. +# Building without wchar_t + +Executive summary: manually define -DQPDF_NO_WCHAR_T in your build if you are building on a system without wchar_t. For details, read the rest of this section. + +While wchar_t is part of the C++ standard library and should be present on virtually every system, there are some stripped down systems, such as those targetting certain embedded environments, that lack wchar_t. Internally, qpdf uses UTF-8 encoding for everything, so there is nothing important in qpdf's API that uses wchar_t. However, there is a helper method for converting between wchar_t* and char* that uses wchar_t. + +If you are building in an environment that does not support wchar_t, you can define the preprocessor symbol QPDF_NO_WCHAR_T in your build. This will work whether you are building qpdf and need to avoid compiling the code that uses wchar_t or whether you are building client code that uses qpdf. + +For example, to build qpdf on a system without wchar_t, be sure that -DQPDF_NO_WCHAR_T is part of your CXXFLAGS. Similar techniques will work in other places. + +Note that, when you build code with libqpdf, it is *not necessary* to have the definition of QPDF_NO_WCHAR_T in your build match what was defined when the library was built as long as you are not calling QUtil::call_main_from_wmain in your code. In other words, if your qpdf library was built on a system without wchar_t and you are using that system to build at some later time after wchar_t was available, as long as you don't call the function that uses it, you can just build normally. + +Note that QPDF_NO_WCHAR_T is never defined by autoconf or any other automated method. There is a hard rule in qpdf that values determined by autoconf are not available in the public API. This is because there is never a guarantee or even expectation that those values will match between the system on which qpdf was build and the system on which a user is building code with libqpdf. + # Building on Windows QPDF is known to build and pass its test suite with mingw (latest version tested: gcc 7.2.0), mingw64 (latest version tested: 7.2.0) and Microsoft Visual C++ 2015, both 32-bit and 64-bit versions. MSYS2 is required to build as well in order to get make and other related tools. See [README-windows.md](README-windows.md) for details on how to build under Windows. diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh index fa6a76aa..2066cfef 100644 --- a/include/qpdf/QUtil.hh +++ b/include/qpdf/QUtil.hh @@ -357,12 +357,20 @@ namespace QUtil QPDF_DLL std::vector parse_numrange(char const* range, int max); +#ifndef QPDF_NO_WCHAR_T + // If you are building qpdf on a stripped down system that doesn't + // have wchar_t, such as may be the case in some embedded + // environments, you may define QPDF_NO_WCHAR_T in your build. + // This symbol is never defined automatically. Search for wchar_t + // in qpdf's top-level README.md file for details. + // Take an argv array consisting of wchar_t, as when wmain is // invoked, convert all UTF-16 encoded strings to UTF-8, and call // another main. QPDF_DLL int call_main_from_wmain(int argc, wchar_t* argv[], std::function realmain); +#endif // QPDF_NO_WCHAR_T }; #endif // QUTIL_HH diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 04aaf51f..8717e148 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -2342,8 +2342,10 @@ QUtil::possible_repaired_encodings(std::string supplied) return t; } +#ifndef QPDF_NO_WCHAR_T int -QUtil::call_main_from_wmain(int argc, wchar_t* argv[], std::function realmain) +QUtil::call_main_from_wmain(int argc, wchar_t* argv[], + std::function realmain) { // argv contains UTF-16-encoded strings with a 16-bit wchar_t. // Convert this to UTF-8-encoded strings for compatibility with @@ -2376,3 +2378,4 @@ QUtil::call_main_from_wmain(int argc, wchar_t* argv[], std::function +#include + +#ifndef QPDF_NO_WCHAR_T +void wmain_test() +{ + auto realmain = [](int argc, char* argv[]) { + for (int i = 0; i < argc; ++i) { + std::cout << argv[i] << std::endl; + } return 0; + }; + wchar_t* argv[3]; + argv[0] = const_cast(L"ascii"); + argv[1] = const_cast(L"10 \xf7 2 = 5"); + argv[2] = const_cast(L"qwww\xf7\x03c0"); + QUtil::call_main_from_wmain(3, argv, realmain); +} +#endif // QPDF_NO_WCHAR_T + +int main(int argc, char* argv[]) +{ +#ifndef QPDF_NO_WCHAR_T + try + { + wmain_test(); + } + catch (std::exception& e) + { + std::cout << "unexpected exception: " << e.what() << std::endl; + } +#endif // QPDF_NO_WCHAR_T + + return 0; +} diff --git a/libtests/qtest/qutil.test b/libtests/qtest/qutil.test index 34b0067c..3487236c 100644 --- a/libtests/qtest/qutil.test +++ b/libtests/qtest/qutil.test @@ -15,4 +15,20 @@ $td->runtest("QUtil", $td->EXIT_STATUS => 0}, $td->NORMALIZE_NEWLINES | $td->RM_WS_ONLY_LINES); -$td->report(1); +my $mfw = `main_from_wmain`; +if ($mfw eq '') +{ + $td->runtest("skipping main_from_wmain", + {$td->STRING => ""}, + {$td->STRING => ""}) +} +else +{ + $td->runtest("main_from_wmain", + {$td->COMMAND => "main_from_wmain"}, + {$td->FILE => "wmain.out", + $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES | $td->RM_WS_ONLY_LINES); +} + +$td->report(2); diff --git a/libtests/qtest/qutil/qutil.out b/libtests/qtest/qutil/qutil.out index 9da8cecf..5fe841ac 100644 --- a/libtests/qtest/qutil/qutil.out +++ b/libtests/qtest/qutil/qutil.out @@ -105,7 +105,3 @@ rename file create file rename over existing delete file ----- wmain -ascii -10 ÷ 2 = 5 -qwww÷π diff --git a/libtests/qtest/qutil/wmain.out b/libtests/qtest/qutil/wmain.out new file mode 100644 index 00000000..261b1c56 --- /dev/null +++ b/libtests/qtest/qutil/wmain.out @@ -0,0 +1,3 @@ +ascii +10 ÷ 2 = 5 +qwww÷π diff --git a/libtests/qutil.cc b/libtests/qutil.cc index b8429a44..935cdfc2 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -543,17 +543,6 @@ void rename_delete_test() assert_no_file("old\xcf\x80.~tmp"); } -void wmain_test() -{ - auto realmain = [](int argc, char* argv[]) { - for (int i = 0; i < argc; ++i) { std::cout << argv[i] << std::endl; } return 0; }; - wchar_t* argv[3]; - argv[0] = const_cast(L"ascii"); - argv[1] = const_cast(L"10 \xf7 2 = 5"); - argv[2] = const_cast(L"qwww\xf7\x03c0"); - QUtil::call_main_from_wmain(3, argv, realmain); -} - int main(int argc, char* argv[]) { try @@ -584,8 +573,6 @@ int main(int argc, char* argv[]) hex_encode_decode_test(); std::cout << "---- rename/delete" << std::endl; rename_delete_test(); - std::cout << "---- wmain" << std::endl; - wmain_test(); } catch (std::exception& e) { diff --git a/manual/qpdf-manual.xml b/manual/qpdf-manual.xml index e22edc2d..83cb6bf9 100644 --- a/manual/qpdf-manual.xml +++ b/manual/qpdf-manual.xml @@ -239,6 +239,16 @@ make Jian Ma. This is also discussed in more detail in README-windows.md. + + While wchar_t is part of the C++ standard, qpdf uses + it in only one place in the public API, and it's just in a helper + function. It is possible to build qpdf on a system that doesn't + have wchar_t, and it's also possible to compile a + program that uses qpdf on a system without wchar_t as + long as you don't call that one method. This is a very unusual + situation. For a detailed discussion, please see the top-level + README.md file in qpdf's source distribution. + There are some other things you can do with the build. Although qpdf uses autoconf, it does not use