libtom / libtommath

LibTomMath is a free open source portable number theoretic multiple-precision integer library written entirely in C.
https://www.libtom.net
Other
645 stars 194 forks source link

tommath_c89.h versus other solutions for backward compatibility with <C99 #526

Open assarbad opened 2 years ago

assarbad commented 2 years ago

The number of makefiles in this project is staggering, but in any case the issue I addressed in #525 for MSVC doesn't seem to be limited to MSVC. Seeing that there is a tommath_c89.h in the source tree, I was wondering if a more generalized approach for any C below C99 wouldn't be the better option at all. In the current draft pull request the header is pretty complete, but has preprocessor conditions which will make it fail on non-MSVC compilers.

Arguably it could be tested on other old compilers (ancient Watcom comes to mind) and the switching could be done based on checking against the standard version, e.g.:

#if (__STDC_VERSION__ < 199901L)
#include <tommath_c89.h> /* an extended version of the current one */
#else
#include <stdint.h>
#include <stdbool.h>
#endif

(or invert the condition order, I don't care)

Either way this would improve the robustness and have all compilers benefit from the C89 compatibility, not just builds done with that makefile where some sed command creates an illusion of compatibility by changing the source files prior to a build (something that in my book is a big no no when building third-party code).


I would also recommend renaming the makefile to GNUmakefile and then consolidating its contents. E.g. for the assignment to OBJECTS it could use something akin to what the pre_gen (amalgamation) target has:

SOURCES:=$(wildcard *mp_*.c)
OBJECTS:=$(SOURCES:.c=.o)

Rationale: if you already have a special makefile for GNU make it makes sense to make this explicit and limit its scope to GNU make. A makefile by the name GNUmakefile will be picked up first by GNU make (it has several names hardcoded), even before makefile. So it could in fact work to rename the current makefile to GNUmakefile then rename the current makefile.unix to makefile. The behavior should be the same, except that on BSD you no longer need to specify the file name.

For the most part when using GNU make I'd also recommend immediate assignments (:=) as opposed to lazy ones (=).

sjaeckel commented 2 years ago

C.f. https://github.com/libtom/libtommath/pull/525#issuecomment-1037980954

assarbad commented 2 years ago

You beat me by it by ~10 minutes. But I hadn't even seen the notification or noticed your update.

Either way, what do you think about the proposals from this ticket, @sjaeckel ? (I'll be patient. Saw the remark on the other ticket.)