Skip to content

Commit

Permalink
move source/bin to bin/ with -DBINDIR
Browse files Browse the repository at this point in the history
for better cross-compilation.
add aarch64 docker files, and cross-compilation support.
  • Loading branch information
rurban committed Feb 29, 2024
1 parent 80fc93b commit a410e77
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 1,086 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
results/
.git/
bin/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ source/algorithms.lst
/data/rand64/rand64.txt
/data/rand8/rand8.txt
/results/

/bin/
20 changes: 20 additions & 0 deletions Dockerfile.cross-aarch64
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- Dockerfile -*-
# podman/docker build -t smart-aarch64 . -f Dockerfile.cross-aarch64

FROM docker.io/library/debian:11-slim as aarch64
ENV DEBIAN_FRONTEND noninteractive
ENV DEBCONF_NONINTERACTIVE_SEEN true
ENV TZ=UTC

RUN apt-get update
RUN apt-get install -y --no-install-recommends build-essential git \
qemu-system-aarch64 binfmt-support qemu-user qemu-user-binfmt \
gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu libc6-dev-arm64-cross
WORKDIR /app
COPY . .
RUN aarch64-linux-gnu-gcc --version
RUN make CC=aarch64-linux-gnu-gcc
RUN ln -s /usr/aarch64-linux-gnu/sys-root/usr/bin/ld.so /lib/ld-linux-aarch64.so.1
ENV LD_LIBRARY_PATH=LD_LIBRARY_PATH=/usr/aarch64-linux-gnu/sys-root/lib64
RUN make CC=aarch64-linux-gnu-gcc check
CMD [ "bash --login -i" ]
32 changes: 32 additions & 0 deletions Dockerfile.fedora-aarch64
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- Dockerfile -*-
# podman/docker build -t smart-fc-aarch64 . -f Dockerfile.fedora-aarch64
ARG BASE_IMAGE=fedora
#FROM ${BASE_IMAGE}
FROM fedora:39-aarch64

# Test with non-root user.
ENV TEST_USER tester
ENV WORK_DIR "/work"

#RUN uname -a
#RUN echo -e "deltarpm=0\ninstall_weak_deps=0\ntsflags=nodocs" >> /etc/dnf/dnf.conf
# Disable modular repositories to save a running time of "dnf update"
#RUN ls /etc/yum.repos.d/*.repo
#RUN sed -i '/^enabled=1$/ s/1/0/' /etc/yum.repos.d/*-modular.repo || true
#RUN dnf -y update
RUN dnf -y --allowerasing --repo fedora install \
file gcc make redhat-rpm-config sudo
#RUN dnf -y --repo fedora install m4 libtool perl-Text-Diff pkgconf-pkg-config strace gdb
#RUN dnf -y --repo fedora install strace gdb

# Create test user and the environment
RUN useradd "${TEST_USER}"
WORKDIR "${WORK_DIR}"
COPY . .
RUN chown -R "${TEST_USER}:${TEST_USER}" "${WORK_DIR}"

# Enable sudo without password for convenience.
RUN echo "${TEST_USER} ALL = NOPASSWD: ALL" >> /etc/sudoers
#RUN ln -s /usr/aarch64-linux-gnu/sys-root/usr/bin/ld.so /lib/ld-linux-aarch64.so.1

USER "${TEST_USER}"
38 changes: 24 additions & 14 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
CC := gcc
uname_m := $(shell $CC -dumpmachine | cut -f1 -d-)
ifneq ($(uname_m),x86_64)
CC := gcc
MACHINE := $(shell uname -m)
ARCH := $(shell ${CC} -dumpmachine | cut -f1 -d-)
ifneq ($(ARCH),x86_64)
CFLAGS := -O3 -Wall
NON_SSE = source/algos/epsm.c source/algos/ssecp.c source/algos/ssef.c
ALGOSRC = $(filter-out $(NON_SSE),$(wildcard source/algos/*.c))
else
CFLAGS := -O3 -march=native -mtune=native -Wall -Wfatal-errors
ALGOSRC = $(wildcard source/algos/*.c)
BINDIR = bin
endif
BINS = $(patsubst source/algos/%,source/bin/%,$(patsubst %.c,%,$(ALGOSRC)))
ifneq ($(ARCH),$(MACHINE))
BINDIR = bin/$(ARCH)
DRV = qemu-$(ARCH)
else
BINDIR = bin
DRV =
endif
BINS = $(patsubst source/algos/%,$(BINDIR)/%,$(patsubst %.c,%,$(ALGOSRC)))
HELPERS = smart show select test textgen compilesm
TESTS = bm mp kmp tbm bom so

all: $(BINS) $(HELPERS)

source/bin/%: source/algos/%.c
$(BINDIR)/%: source/algos/%.c
@test -d $(BINDIR) || mkdir $(BINDIR)
$(CC) $(CFLAGS) $< -o $@
./%: source/%.c source/*.h
$(CC) $(CFLAGS) $< -std=gnu99 -o $@ -lm
$(CC) $(CFLAGS) -DBINDIR="\"$(BINDIR)\"" $< -std=gnu99 -o $@ -lm
select: source/selectAlgo.c
$(CC) $(CFLAGS) $< -o $@
$(CC) $(CFLAGS) -DBINDIR="\"$(BINDIR)\"" $< -o $@

.PHONY: check clean all
check: all
./select -which | grep br
-cp source/bin/br source/bin/br1
./select -add br1
-rm source/bin/br1
./select -none $(TESTS)
./smart -text rand32
$(DRV) ./select -which | grep br
-cp $(BINDOR)/br $(BINDOR)/br1
$(DRV) ./select -add br1
-rm $(BINDOR)/br1
$(DRV) ./select -none $(TESTS)
$(DRV) ./smart -text rand32
for t in $(TESTS); do echo $$t; ./test $$t; done
./select -all block bmh2 bmh4 dfdm sbdm faoso2 blim ssecp
$(DRV) ./select -all block bmh2 bmh4 dfdm sbdm faoso2 blim ssecp

clean:
rm $(BINS) $(HELPERS)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The release of smart will be available [here](https://github.com/smart-tool/smar
The documentation about smart is available [here](https://www.dmi.unict.it/faro/smart/howto.php)

## How to compile it from source
To compile the source just download (or clone) this repository and run the file build.sh from terminal (with **./build.sh**), it will compile the smart binaries and all the algorithms (the algorithms binaries will be created into source/bin/).
To compile the source just download (or clone) this repository and run the file build.sh from terminal (with **./build.sh**), it will compile the smart binaries and all the algorithms (the algorithms binaries will be created into bin/).

## Reference

Expand Down
File renamed without changes.
24 changes: 16 additions & 8 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/sh
./logo
CC="${CC:-gcc}"
uname_p="$(uname -p)"
if [ $uname_p = x86_64 ]
MACHINE=`uname -m`
ARCH=`${CC} -dumpmachine | cut -f1 -d-`
if [ $MACHINE = x86_64 ]
then
CFLAGS="-march=native -mtune=native"
# we don't care that much about windows non-compat warnings yet
Expand All @@ -15,19 +16,26 @@ then
esac
SSE2="-msse2"
fi
BINDIR=bin
DRV=
if [ "$ARCH" != "MACHINE" ]; then
CROSS=1
BINDIR=bin/$ARCH
DRV="qemu-$(ARCH) "
fi
echo " Compiling smart.c..................................[OK]"
$CC source/smart.c -O3 $CFLAGS $SSE2 -o smart -lm -std=gnu99
$CC source/smart.c -O3 $CFLAGS $SSE2 -o smart -lm -std=gnu99 -DBINDIR="\"$BINDIR\""
echo " Compiling show.c...................................[OK]"
$CC source/show.c -O3 $CFLAGS $SSE2 -o show
echo " Compiling selectAlgo.c.............................[OK]"
$CC source/selectAlgo.c -O3 $CFLAGS -o select
$CC source/selectAlgo.c -O3 $CFLAGS -o select -DBINDIR="\"$BINDIR\""
echo " Compiling test.c...................................[OK]"
$CC source/test.c -O3 $CFLAGS -o test
$CC source/test.c -O3 $CFLAGS -o test -DBINDIR="\"$BINDIR\""
echo " Generating random text buffers....................."
$CC source/textgen.c -o textgen -std=gnu99
./textgen
$DRV./textgen
echo " Compiling all string matching algorithms..........."
$CC source/compilesm.c -o compilesm -std=gnu99
./compilesm
$CC source/compilesm.c -o compilesm -std=gnu99 -DBINDIR="\"$BINDIR\""
$DRV./compilesm
echo " "
echo " "
Binary file removed source/a.out
Binary file not shown.
44 changes: 29 additions & 15 deletions source/compilesm.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,56 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

/*
* This program compiles all c programs of string matching algorithms
* It is called by build.sh script, when compiling SMART
*/

int main(int argc, char **argv) {
char filename[100], command[512], binary[100];
int i;
char destination[100] = "source/bin/";
char filename[100], command[512], binary[100];
char destdir[100] = "bin/";
char gcc[100] = "gcc source/algos/";
char options[100] = " -O3";
#ifndef _WIN32
strcat(options, " -Wall");
#endif
#ifdef __x86_64__
strcat(options, " -march=native -mtune=native -msse4");
strcat(options, " -march=native -mtune=native");
#endif
strcat(options, " -lm -o source/bin/");

#ifdef BINDIR
strncpy(destdir, BINDIR, sizeof(destdir) - 2);
strncat(destdir, "/", sizeof(destdir) - 1);
# ifndef _WIN32
mkdir(destdir, 0775);
# else
mkdir(destdir);
# endif
#endif

int doTest = 0;
if (argc > 1) {
if (!strcmp(argv[1], "dotest"))
if (!strcmp(argv[1], "dotest")) {
doTest = 1;
}
}
strcat(options, " -lm");

DIR *d;
FILE *stream;
struct dirent *dir;

// delete previous compiled files
d = opendir("./source/bin");
d = opendir(destdir);
if (d) {
while ((dir = readdir(d)) != NULL) {
strcpy(filename, dir->d_name);
if (strcmp(filename, ".gitignore") == 0)
if (*filename && *filename == '.')
continue;
sprintf(command, "./source/bin/%s", filename);
snprintf(command, sizeof(command)-1, "%s%s", destdir, filename);
remove(command);
}
}
Expand All @@ -71,7 +83,7 @@ int main(int argc, char **argv) {
while ((dir = readdir(d)) != NULL) {
strcpy(filename, dir->d_name);
int len = strlen(filename);
if (filename[len - 1] == 'c' && filename[len - 2] == '.') {
if (len > 2 && filename[len - 1] == 'c' && filename[len - 2] == '.') {
n_algo++;
}
}
Expand All @@ -86,10 +98,11 @@ int main(int argc, char **argv) {
while ((dir = readdir(d)) != NULL) {
strcpy(filename, dir->d_name);
int len = strlen(filename);
if (filename[len - 1] == 'c' && filename[len - 2] == '.') {
if (len > 2 && filename[len - 1] == 'c' && filename[len - 2] == '.') {
filename[len - 2] = '\0';
current++;
#ifndef __x86_64__
#if !defined __x86_64__ || !defined __SSE__
// skip SSE specific algos
if (!strcmp(filename, "epsm") ||
!strcmp(filename, "ssecp") ||
!strcmp(filename, "ssef")) {
Expand All @@ -98,7 +111,8 @@ int main(int argc, char **argv) {
}
#endif
// compile
sprintf(command, "%s%s.c%s%s", gcc, filename, options, filename);
snprintf(command, sizeof(command)-1, "%s%s.c%s -o %s%s", gcc,
filename, options, destdir, filename);
if (doTest)
printf("\tCompiling and testing %s.c", filename);
else
Expand All @@ -116,11 +130,11 @@ int main(int argc, char **argv) {
printf("[ERROR]\n");
else {
// check if compiled file is present
if (strlen(destination) + strlen(filename) >= sizeof(binary)) {
if (strlen(destdir) + strlen(filename) >= sizeof(binary)) {
perror("snprintf");
exit(1);
}
strncpy(binary, destination, sizeof(binary) - 1);
strncpy(binary, destdir, sizeof(binary) - 1);
strncat(binary, filename, sizeof(binary) - 1);
FILE *fp = fopen(binary, "r");
if (fp) {
Expand All @@ -129,7 +143,7 @@ int main(int argc, char **argv) {
fflush(stdout);
if (doTest) {
// testing correctness of the algorithm
sprintf(command, "./test %s -nv", filename);
snprintf(command, sizeof(command)-1, "./test %s -nv", filename);
// printf("\b\b\b\b\b[000%]");
fflush(stdout);
if (system(command)) {
Expand Down
Loading

0 comments on commit a410e77

Please sign in to comment.