Skip to content

Commit

Permalink
Rework demos building and installation
Browse files Browse the repository at this point in the history
* add `ltc` wrapper script for installed demos
* rework demos/CMakeLists.txt and add some more bells and whistles
* prefix installed demos with `ltc-`

Currently this makes the standard makefiles and CMake results diverge, but
we can fix that later if required.

Signed-off-by: Steffen Jaeckel <[email protected]>
  • Loading branch information
sjaeckel committed Dec 5, 2024
1 parent a9bb1c1 commit 88dcebd
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ doc/crypt.pdf
doc/refman.pdf

# *nix/windows test executables
ltc-*
aesgcm
aesgcm.exe
constants
Expand Down
104 changes: 61 additions & 43 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,97 @@
# -----------------------------------------------------------------------------
option(BUILD_USEFUL_DEMOS "Build useful demos (hashsum)" FALSE)
option(BUILD_USABLE_DEMOS "Build usable demos (crypt sizes constants pem-info)" FALSE)
option(BUILD_BROKEN_DEMOS "Build broken demos (aesgcm openssh-privkey openssl-enc timing)" FALSE)
option(BUILD_TEST_DEMOS "Build test demos (small tv_gen)" FALSE)

option(INSTALL_DEMOS "Install enabled demos (USEFUL and/or USABLE) and ltc wrapper script" FALSE)
option(INSTALL_BROKEN_DEMOS "Install broken demos and ltc wrapper script" FALSE)

# -----------------------------------------------------------------------------
# Useful demos
#
# Demos that are even somehow useful and could be installed as a system-tool
#
# * USEFUL_DEMOS = hashsum
# -----------------------------------------------------------------------------

if(BUILD_USEFUL_DEMOS)

list(APPEND ALL_DEMOS_TARGETS hashsum)

# hashsum
add_executable(hashsum ${CMAKE_CURRENT_SOURCE_DIR}/hashsum.c)

target_link_libraries(hashsum PRIVATE ${PROJECT_NAME})

list(APPEND USABLE_DEMOS_TARGETS hashsum)
endif()

# -----------------------------------------------------------------------------
# Usable demos
#
# Demos that are usable but only rarely make sense to be installed
#
# USEABLE_DEMOS = crypt sizes constants pem-info
# -----------------------------------------------------------------------------

if(BUILD_USABLE_DEMOS)
list(APPEND USABLE_DEMOS_TARGETS crypt sizes constants pem-info)
endif()

list(APPEND ALL_DEMOS_TARGETS crypt sizes constants pem-info)

# ltcrypt
add_executable(crypt ${CMAKE_CURRENT_SOURCE_DIR}/crypt.c)

target_link_libraries(crypt PRIVATE ${PROJECT_NAME})

# sizes
add_executable(sizes ${CMAKE_CURRENT_SOURCE_DIR}/sizes.c)

target_link_libraries(sizes PRIVATE ${PROJECT_NAME})

# constants
add_executable(constants ${CMAKE_CURRENT_SOURCE_DIR}/constants.c)

target_link_libraries(constants PRIVATE ${PROJECT_NAME})

# pem-info
add_executable(pem-info ${CMAKE_CURRENT_SOURCE_DIR}/pem-info.c)

target_link_libraries(pem-info PRIVATE ${PROJECT_NAME})
# -----------------------------------------------------------------------------
# Broken demos
#
# Demos that are kind of useful, but in some way broken
#
# * aesgcm - can't be built with LTC_EASY
# * openssl-enc - can't be built with LTC_EASY
# * openssh-privkey - can't be built with LTC_EASY
# * timing - not really broken, but older gcc builds spit warnings
#
# BROKEN_DEMOS = aesgcm openssl-enc openssh-privkey timing
# -----------------------------------------------------------------------------

if(BUILD_BROKEN_DEMOS AND INSTALL_BROKEN_DEMOS)
list(APPEND USABLE_DEMOS_TARGETS aesgcm openssh-privkey openssl-enc timing)
elseif(BUILD_BROKEN_DEMOS)
list(APPEND ALL_DEMOS_TARGETS aesgcm openssh-privkey openssl-enc timing)
endif()

# -----------------------------------------------------------------------------
# Test demos
#
# Demos that are used for testing or measuring
#
# * TEST_DEMOS = small tv_gen
# -----------------------------------------------------------------------------

if(BUILD_TEST_DEMOS)
list(APPEND ALL_DEMOS_TARGETS small tv_gen)
endif()

list(APPEND ALL_DEMOS_TARGETS tv_gen)
# -----------------------------------------------------------------------------
# Generate executables
# -----------------------------------------------------------------------------

# small
add_executable(small ${CMAKE_CURRENT_SOURCE_DIR}/small.c)
# USABLE_DEMOS can get installed, so they're prefixed with `ltc-`
foreach(target ${USABLE_DEMOS_TARGETS})
list(APPEND ALL_DEMOS_INSTALL_TARGETS ltc-${target})

target_link_libraries(small PRIVATE ${PROJECT_NAME})
add_executable(ltc-${target} ${CMAKE_CURRENT_SOURCE_DIR}/${target}.c)

# tv_gen
add_executable(tv_gen ${CMAKE_CURRENT_SOURCE_DIR}/tv_gen.c)
target_link_libraries(ltc-${target} PRIVATE ${PROJECT_NAME})
endforeach()

target_link_libraries(tv_gen PRIVATE ${PROJECT_NAME})
foreach(target ${ALL_DEMOS_TARGETS})
add_executable(${target} ${CMAKE_CURRENT_SOURCE_DIR}/${target}.c)

endif()
target_link_libraries(${target} PRIVATE ${PROJECT_NAME})
endforeach()

# -----------------------------------------------------------------------------
# Install targets
# -----------------------------------------------------------------------------
install(
TARGETS ${ALL_DEMOS_TARGETS}
COMPONENT "runtime"
EXPORT ${TARGETS_EXPORT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(INSTALL_DEMOS)
install(
TARGETS ${ALL_DEMOS_INSTALL_TARGETS}
COMPONENT "runtime"
EXPORT ${TARGETS_EXPORT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# Also install the `ltc` wrapper script
install(PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/ltc DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
34 changes: 34 additions & 0 deletions demos/ltc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

RELDIR="/$0"
RELDIR="${RELDIR%/*}"
RELDIR="${RELDIR:-.}"
RELDIR="${RELDIR##/}/"

BINDIR=`cd "$RELDIR"; pwd`

err_out() {
err=$1
shift
echo $* >&2
exit $err
}

usage() {
cat >&$(($1 + 1)) << EOF
Available commands are:
`ls -1 $BINDIR/ltc-* | sed "s@$BINDIR/ltc-@ @g"`
help
EOF
exit $1
}

[ $# -gt 0 ] || usage 1

TOOL="$1"
shift
[ "$TOOL" == "help" ] || [ "$TOOL" == "--help" ] || [ "$TOOL" == "-h" ] && usage 0

test -x "$BINDIR/ltc-$TOOL" || err_out 1 "Unknown command: $TOOL"

[ $# -gt 0 ] && "$BINDIR/ltc-$TOOL" "$@" || "$BINDIR/ltc-$TOOL"
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ $(1): $(call print-help,$(1),Builds the library and the '$(1)' demo) demos/$(1).
ifneq ($V,1)
@echo " * $${CC} $$@" ${silent_echo}
endif
$${silent} $$(CC) $$(LTC_LDFLAGS) $$< $$(LIB_PRE) $$(LIBNAME) $$(LIB_POST) $$(LTC_EXTRALIBS) -o $(1)
$${silent} $$(CC) $$(LTC_LDFLAGS) $$< $$(LIB_PRE) $$(LIBNAME) $$(LIB_POST) $$(LTC_EXTRALIBS) -o $$@
endef

$(foreach demo, $(strip $(DEMOS)), $(eval $(call DEMO_template,$(demo))))
Expand Down
4 changes: 2 additions & 2 deletions makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ install: $(LIBMAIN_S) $(LIBMAIN_I) $(LIBMAIN_D)
copy /Y src\headers\tomcrypt*.h "$(PREFIX)\include"

#Install useful tools
install_bins: hashsum
install_bins: hashsum.exe
cmd /c if not exist "$(PREFIX)\bin" mkdir "$(PREFIX)\bin"
copy /Y hashsum.exe "$(PREFIX)\bin"
copy /Y hashsum.exe "$(PREFIX)\bin\ltc-hashsum.exe"

#Install documentation
install_docs: doc/crypt.pdf
Expand Down
4 changes: 2 additions & 2 deletions makefile.msvc
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ install: $(LIBMAIN_S)
copy /Y src\headers\tomcrypt*.h "$(PREFIX)\include"

#Install useful tools
install_bins: hashsum
install_bins: hashsum.exe
cmd /c if not exist "$(PREFIX)\bin" mkdir "$(PREFIX)\bin"
copy /Y hashsum.exe "$(PREFIX)\bin"
copy /Y hashsum.exe "$(PREFIX)\bin\ltc-hashsum.exe"

#Install documentation
install_docs: doc/crypt.pdf
Expand Down
2 changes: 1 addition & 1 deletion makefile.shared
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ test: $(call print-help,test,Builds the library and the 'test' application to ru
# build the demos from a template
define DEMO_template
$(1): $(call print-help,$(1),Builds the library and the '$(1)' demo) demos/$(1).o $$(LIBNAME)
$$(TGTLIBTOOL) --mode=link --tag=CC $$(CC) $$(LTC_LDFLAGS) $$^ $$(EXTRALIBS) -o $(1)
$$(TGTLIBTOOL) --mode=link --tag=CC $$(CC) $$(LTC_LDFLAGS) $$^ $$(EXTRALIBS) -o $$@
endef

$(foreach demo, $(strip $(DEMOS)), $(eval $(call DEMO_template,$(demo))))
Expand Down
3 changes: 2 additions & 1 deletion makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ install: $(LIBMAIN_S)
#Install useful tools
install_bins: hashsum
@mkdir -p $(DESTDIR)$(BINPATH)
@cp hashsum $(DESTDIR)$(BINPATH)/
@cp hashsum $(DESTDIR)$(BINPATH)/ltc-hashsum
@cp demos/ltc $(DESTDIR)$(BINPATH)/ltc

#Install documentation
install_docs: doc/crypt.pdf
Expand Down
6 changes: 5 additions & 1 deletion makefile_include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ endif
ifneq ($(shell echo $(CFLAGS) | grep TFM_DESC),)
LTC_CFLAGS+=$(shell PKG_CONFIG_PATH=$(LIBPATH)/pkgconfig pkg-config --cflags-only-I tomsfastmath)
endif
ifneq ($(shell echo $(CFLAGS) | grep GMP_DESC),)
LTC_CFLAGS+=$(shell PKG_CONFIG_PATH=$(LIBPATH)/pkgconfig pkg-config --cflags-only-I gmp)
endif
LTC_CFLAGS += -I./src/headers/ -DLTC_SOURCE -Wall -Wsign-compare -Wshadow

ifdef OLD_GCC
Expand Down Expand Up @@ -485,7 +488,8 @@ $(DESTDIR)$(BINPATH):
install -p -d $(DESTDIR)$(BINPATH)

.common_install_bins: $(USEFUL_DEMOS) $(DESTDIR)$(BINPATH)
$(INSTALL_CMD) -p -m 775 $(USEFUL_DEMOS) $(DESTDIR)$(BINPATH)
for d in $(USEFUL_DEMOS); do $(INSTALL_CMD) -p -m 775 $$d $(DESTDIR)$(BINPATH)/ltc-$$d
$(INSTALL_CMD) -p -m 775 demos/ltc $(DESTDIR)$(BINPATH)

install_docs: $(call print-help,install_docs,Installs the Developer Manual) doc/crypt.pdf
install -p -d $(DESTDIR)$(DATAPATH)
Expand Down

0 comments on commit 88dcebd

Please sign in to comment.