Skip to content

Instantly share code, notes, and snippets.

@rahogata
Last active June 28, 2025 14:30
Show Gist options
  • Save rahogata/4d21131718f67e4aca577a08dcf2a06b to your computer and use it in GitHub Desktop.
Save rahogata/4d21131718f67e4aca577a08dcf2a06b to your computer and use it in GitHub Desktop.
Build redisearch module in freebsd 13

Install build dependencies

pkg install -y bash wget git cmake gmake gcc11 pkgconf python311 py311-pipenv py311-pip openssl

Clone a specific release

git clone --branch v2.8.15 --depth 1 --recursive https://github.com/RediSearch/RediSearch.git

Enter project directory

cd RediSearch

Change boost url

sed -i '' 's|https://boostorg.jfrog.io/artifactory/main|https://archives.boost.io|' .install/install_boost.sh

Install boost

cd .install && bash ./install_boost.sh 1.84.0 && cd -

Add symlinks for python

ln -s /usr/local/bin/python3.11 /usr/local/bin/python3
ln -s /usr/local/bin/gcc11 /usr/bin/gcc
ln -s /usr/local/bin/bash /bin/bash

Fix Compilation Errors

Fix errors in deps/thpool/thpool.c by changing LOG_IF_EXISTS lines. Use below patch file as a reference.

diff --git a/deps/thpool/thpool.c b/deps/thpool/thpool.c
index 7423962a2..7957a1899 100644
--- a/deps/thpool/thpool.c
+++ b/deps/thpool/thpool.c
@@ -33,7 +33,7 @@
 #define THPOOL_DEBUG 0
 #endif
 
-#define LOG_IF_EXISTS(level, str, ...) if (thpool_p->log) {thpool_p->log(level, str, ##__VA_ARGS__);}
+#define LOG_IF_EXISTS(thpool_p,level, str, ...) if (thpool_p->log) {thpool_p->log(level, str, ##__VA_ARGS__);}
 
 static volatile int threads_on_hold;
 
@@ -145,7 +145,7 @@ struct redisearch_thpool_t* redisearch_thpool_create(size_t num_threads, size_t
   /* Initialise the job queue */
   if (num_privileged_threads > num_threads) num_privileged_threads = num_threads;
   if(priority_queue_init(&thpool_p->jobqueue, num_threads, num_privileged_threads) == -1) {
-    LOG_IF_EXISTS("warning", "redisearch_thpool_create(): Could not allocate memory for job queue")
+    LOG_IF_EXISTS(thpool_p,"warning", "redisearch_thpool_create(): Could not allocate memory for job queue")
     rm_free(thpool_p);
     return NULL;
   }
@@ -153,7 +153,7 @@ struct redisearch_thpool_t* redisearch_thpool_create(size_t num_threads, size_t
   /* Make threads in pool */
   thpool_p->threads = (struct thread**)rm_malloc(num_threads * sizeof(struct thread*));
   if (thpool_p->threads == NULL) {
-    LOG_IF_EXISTS("warning", "redisearch_thpool_create(): Could not allocate memory for threads")
+    LOG_IF_EXISTS(thpool_p,"warning", "redisearch_thpool_create(): Could not allocate memory for threads")
     priority_queue_destroy(&thpool_p->jobqueue);
     rm_free(thpool_p);
     return NULL;
@@ -162,7 +162,7 @@ struct redisearch_thpool_t* redisearch_thpool_create(size_t num_threads, size_t
   for (size_t i = 0; i < num_threads; i++) {
     thpool_p->threads[i] = (struct thread*)rm_malloc(sizeof(struct thread));
     if (thpool_p->threads[i] == NULL) {
-	  LOG_IF_EXISTS("warning", "thread_create(): Could not allocate memory for thread")
+	  LOG_IF_EXISTS(thpool_p,"warning", "thread_create(): Could not allocate memory for thread")
       priority_queue_destroy(&thpool_p->jobqueue);
       for (size_t j = 0; j < i; j++) {
         rm_free(thpool_p->threads[j]);
@@ -193,7 +193,7 @@ void redisearch_thpool_init(struct redisearch_thpool_t* thpool_p) {
   while (thpool_p->num_threads_alive != thpool_p->total_threads_count) {
     usleep(1); // avoid busy loop, wait for a very small amount of time.
   }
-  LOG_IF_EXISTS("verbose", "Thread pool of size %zu created successfully",
+  LOG_IF_EXISTS(thpool_p,"verbose", "Thread pool of size %zu created successfully",
                 thpool_p->total_threads_count)
 }
 
@@ -203,7 +203,7 @@ int redisearch_thpool_add_work(redisearch_thpool_t* thpool_p, void (*function_p)
 
   newjob = (struct job*)rm_malloc(sizeof(struct job));
   if (newjob == NULL) {
-    LOG_IF_EXISTS("warning", "thpool_add_work(): Could not allocate memory for new job");
+    LOG_IF_EXISTS(thpool_p,"warning", "thpool_add_work(): Could not allocate memory for new job");
     return -1;
   }
 
@@ -248,7 +248,7 @@ int redisearch_thpool_add_n_work(redisearch_threadpool thpool_p, redisearch_thpo
   return 0;
 
 fail:
-  LOG_IF_EXISTS("warning", "redisearch_thpool_add_n_work(): Could not allocate memory for %zu new jobs", n_jobs);
+  LOG_IF_EXISTS(thpool_p,"warning", "redisearch_thpool_add_n_work(): Could not allocate memory for %zu new jobs", n_jobs);
   while (first_newjob) {
     job* tmp = first_newjob->prev;
     rm_free(first_newjob);
@@ -436,13 +436,15 @@ static void* thread_do(struct thread* thread_p) {
   prctl(PR_SET_NAME, thread_name);
 #elif defined(__APPLE__) && defined(__MACH__)
   pthread_setname_np(thread_name);
+#elif defined(__FreeBSD__)
+  pthread_setname_np(pthread_self(), thread_name);
 #else
-	LOG_IF_EXISTS("warning", "thread_do(): pthread_setname_np is not supported on this system")
+	LOG_IF_EXISTS(thread_p->thpool_p,"warning", "thread_do(): pthread_setname_np is not supported on this system")
 #endif
 
   /* Assure all threads have been created before starting serving */
   redisearch_thpool_t* thpool_p = thread_p->thpool_p;
-  LOG_IF_EXISTS("verbose", "Creating background thread-%d", thread_p->id)
+  LOG_IF_EXISTS(thpool_p,"verbose", "Creating background thread-%d", thread_p->id)
 
   /* Register signal handler */
   struct sigaction act;
@@ -450,7 +452,7 @@ static void* thread_do(struct thread* thread_p) {
   act.sa_flags = 0;
   act.sa_handler = thread_hold;
   if (sigaction(SIGUSR2, &act, NULL) == -1) {
-    LOG_IF_EXISTS("warning", "thread_do(): cannot handle SIGUSR1")
+    LOG_IF_EXISTS(thpool_p,"warning", "thread_do(): cannot handle SIGUSR1")
   }
 
   /* Mark thread as alive (initialized) */
@@ -461,7 +463,7 @@ static void* thread_do(struct thread* thread_p) {
   while (thpool_p->keepalive) {
 
     bsem_wait(thpool_p->jobqueue.has_jobs);
-    LOG_IF_EXISTS("debug", "Thread-%d is running iteration", thread_p->id)
+    LOG_IF_EXISTS(thpool_p,"debug", "Thread-%d is running iteration", thread_p->id)
     if (thpool_p->keepalive) {
 
       pthread_mutex_lock(&thpool_p->thcount_lock);
@@ -483,18 +485,18 @@ static void* thread_do(struct thread* thread_p) {
       thpool_p->num_threads_working--;
       if (job_p) thpool_p->total_jobs_done++;
       if (thpool_p->num_threads_working == 0) {
-	      LOG_IF_EXISTS("debug", "All threads are idle")
+	      LOG_IF_EXISTS(thpool_p,"debug", "All threads are idle")
 	      pthread_cond_signal(&thpool_p->threads_all_idle);
       }
 	  if (priority_queue_len(&thpool_p->jobqueue) == 0 && thpool_p->terminate_when_empty) {
-		  LOG_IF_EXISTS("verbose", "Job queue is empty - terminating thread %d", thread_p->id);
+		  LOG_IF_EXISTS(thpool_p,"verbose", "Job queue is empty - terminating thread %d", thread_p->id);
           thpool_p->keepalive = 0;
 	  }
       pthread_mutex_unlock(&thpool_p->thcount_lock);
     }
   }
   pthread_mutex_lock(&thpool_p->thcount_lock);
-  LOG_IF_EXISTS("verbose", "Terminating thread %d", thread_p->id)
+  LOG_IF_EXISTS(thpool_p,"verbose", "Terminating thread %d", thread_p->id)
   thpool_p->num_threads_alive--;
   pthread_mutex_unlock(&thpool_p->thcount_lock);
 

Build

gmake build FORCE=1 DEBUG=0 TESTS=0 CMAKE_ARGS="-DCMAKE_C_COMPILER=gcc11 -DCMAKE_CXX_COMPILER=g++11 -DCMAKE_C_FLAGS='-Wl,-rpath=/usr/local/lib/gcc11 -include stdint.h -DCLOCK_MONOTONIC_RAW=CLOCK_MONOTONIC -Du_int64_t=uint64_t -Dpthread_setname_np=pthread_set_name_np' -DCMAKE_CXX_FLAGS='-Wl,-rpath=/usr/local/lib/g++11 -include stdint.h -DCLOCK_MONOTONIC_RAW=CLOCK_MONOTONIC -Du_int64_t=uint64_t'"

Note

If the below error is encountered run the build command again.

gmake[3]: /RediSearch/deps/readies/mk/extract-obj-symbols: No such file or directory
gmake[3]: *** [CMakeFiles/redisearch.dir/build.make:555: redisearch.so] Error 127
gmake[2]: *** [CMakeFiles/Makefile2:343: CMakeFiles/redisearch.dir/all] Error 2
gmake[1]: *** [Makefile:136: all] Error 2

Binary Generated

ls -lh bin/freebsd-x64-release/search/redisearch.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment