Skip to content

Commit

Permalink
Merge pull request #40 from tritonuas/feat/cmake-war
Browse files Browse the repository at this point in the history
feat/cmake war
  • Loading branch information
atar13 authored Oct 26, 2023
2 parents 0895aad + 8172d93 commit f166071
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 20 deletions.
25 changes: 22 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@ FATAL: In-source builds are not allowed.
")
endif()

# Check if Ninja is available
find_program(NINJA_EXECUTABLE ninja)

cmake_minimum_required(VERSION 3.13)
# Set the default generator
if(NINJA_EXECUTABLE)
set(CMAKE_GENERATOR "Ninja")
else()
set(CMAKE_GENERATOR "Unix Makefiles") # Use a fallback generator
endif()

# enable build caching
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif()

cmake_minimum_required(VERSION 3.13 FATAL_ERROR)


set(CMAKE_CXX_COMPILER g++)
Expand All @@ -25,8 +41,8 @@ file(GLOB_RECURSE HEADERS "include/*.hpp")
# =============================
# Executable
set(INCLUDE_DIRECTORY ${PROJECT_SOURCE_DIR}/include)
include_directories(${INCLUDE_DIRECTORY})
add_executable(${PROJECT_NAME} ${SOURCES})
include_directories(${INCLUDE_DIRECTORY})
# =============================

# =============================
Expand All @@ -36,7 +52,9 @@ set(DEPS_DIRECTORY ${PROJECT_SOURCE_DIR}/deps)
add_subdirectory(${DEPS_DIRECTORY}/arena-sdk)
add_subdirectory(${DEPS_DIRECTORY}/json)
# =============================

include(${DEPS_DIRECTORY}/torch/CMakeLists.txt)
# NOTE: temporarily leaving out opencv until we have it working in dev container
# include(${DEPS_DIRECTORY}/opencv/CMakeLists.txt)
# =============================
# Unit tests
add_subdirectory(tests)
Expand All @@ -46,6 +64,7 @@ add_subdirectory(${DEPS_DIRECTORY}/google-test)
# =============================
# Linting
get_target_property(PROJECT_LIBS_INCLUDE_DIRS ${PROJECT_NAME} INCLUDE_DIRECTORIES)

foreach(dir ${PROJECT_LIBS_INCLUDE_DIRS})
string(APPEND LIB_INCLUDE_CLANG_TIDY_STRING "-I${dir};")
endforeach()
Expand Down
48 changes: 46 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
FROM ubuntu:22.04
# https://gist.github.com/SSARCandy/fc960d8905330ac695e71e3f3807ce3d
# OpenCV dependencies from above
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y build-essential \
gdb \
git \
cmake \
clang-tidy \
wget
wget \
ccache \
vim
# cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
# python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

# the official docs say also these
# https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html
# sudo apt update && sudo apt install -y cmake g++ wget unzip
# xz? bzip2?

# TODO: is it possible to save the built opencv in the docker build? Need to see what cmake keeps checking and wasting time on.

RUN apt-get update \
&& rm -rf /var/lib/apt/lists/* \
&& wget https://github.com/Kitware/CMake/releases/download/v3.27.7/cmake-3.27.7-linux-x86_64.sh \
-q -O /tmp/cmake-install.sh \
&& chmod u+x /tmp/cmake-install.sh \
&& mkdir /opt/cmake-3.24.1 \
&& /tmp/cmake-install.sh --skip-license --prefix=/opt/cmake-3.24.1 \
&& rm /tmp/cmake-install.sh \
&& ln -s /opt/cmake-3.24.1/bin/* /usr/local/bin

WORKDIR /obcpp

COPY . /obcpp

WORKDIR /obcpp/build

# RUN cmake ..

# RUN make obcpp # parallelize this

CMD ["/bin/bash"]
# CMD ["/obcpp/build/bin/obcpp"]

# docker build - < Dockerfile
# docker build -t "test" .
# docker run -it --rm test

# https://www.jmoisio.eu/en/blog/2020/06/01/building-cpp-containers-using-docker-and-cmake/
# find -name "*Cache.txt" -delete

# TODO: this container is way too big for some reason. Refer to above blog post for staged build (may not want to do that for development tho. I am unable to get devcontainers running)
4 changes: 2 additions & 2 deletions deps/arena-sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ if(UNIX AND NOT APPLE)
"${ARENA_SDK_DIR}/extracted/GenICam/library/CPP/include"
)
file(GLOB_RECURSE ARENA_LIBS
"${ARENA_SDK_DIR}/extracted/lib64/*.so"
"${ARENA_SDK_DIR}/extracted/ffmpeg/*.so"
# "${ARENA_SDK_DIR}/extracted/lib64/*.so"
"${ARENA_SDK_DIR}/extracted/**/*.so"
)
target_link_libraries(${PROJECT_NAME} PRIVATE
${ARENA_LIBS}
Expand Down
43 changes: 43 additions & 0 deletions deps/opencv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
include(FetchContent)

# Fetch the main OpenCV repository
FetchContent_Declare(OpenCV
URL https://github.com/opencv/opencv/archive/refs/tags/4.8.1.tar.gz
OVERRIDE_FIND_PACKAGE
)

# set( OPENCV_EXTRA_MODULES_PATH ${OpenCV_SOURCE_DIR}/../opencvcontrib-src/modules)
# Fetch the OpenCV contrib repository
FetchContent_Declare(OpenCVContrib
URL https://github.com/opencv/opencv_contrib/archive/refs/tags/4.8.1.tar.gz
# SOURCE_DIR "${CMAKE_BINARY_DIR}/opencv_contrib-src" # Specify a separate source directory
# PREFIX "${CMAKE_BINARY_DIR}/opencv_contrib-prefix" # Specify a separate prefix directory
)

# Make both OpenCV and OpenCV contrib available
FetchContent_MakeAvailable(OpenCV OpenCVContrib)

# Find OpenCV
find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui features2d flann dnn imgcodecs videoio imgproc ml)
set(OpenCV_INCLUDE_DIRS ${OpenCV_SOURCE_DIR}/include )
# file(GLOB_RECURSE HEADERS "${OpenCV_SOURCE_DIR}/modules/**/*.hpp")
# Add OpenCV's contrib modules to the target
target_link_libraries(${PROJECT_NAME} PRIVATE ${OpenCV_LIBS} opencv_core opencv_highgui opencv_features2d opencv_flann opencv_imgcodecs opencv_dnn opencv_videoio opencv_imgproc opencv_ml )

# Specify include directories, including OpenCV's contrib
target_include_directories(${PROJECT_NAME} PRIVATE
${OpenCV_INCLUDE_DIRS}
${OpenCVContrib_SOURCE_DIR}/modules # Include contrib modules
${OPENCV_CONFIG_FILE_INCLUDE_DIR}
${OPENCV_MODULE_opencv_core_LOCATION}/include
${OPENCV_MODULE_opencv_highgui_LOCATION}/include
${OPENCV_MODULE_opencv_features2d_LOCATION}/include

${OPENCV_MODULE_opencv_flann_LOCATION}/include
${OPENCV_MODULE_opencv_imgcodecs_LOCATION}/include
${OPENCV_MODULE_opencv_dnn_LOCATION}/include
${OPENCV_MODULE_opencv_videoio_LOCATION}/include
${OPENCV_MODULE_opencv_imgproc_LOCATION}/include
${OPENCV_MODULE_opencv_ml_LOCATION}/include
# ${HEADERS}
)
34 changes: 34 additions & 0 deletions deps/torch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
include(FetchContent)
Set(FETCHCONTENT_QUIET FALSE)

FetchContent_Declare(Torch
URL https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.1.0%2Bcpu.zip
)
# URL https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip

FetchContent_MakeAvailable(Torch)

# message(FATAL_ERROR ${torch_SOURCE_DIR})
# set(Torch_DIR "${CMAKE_BINARY_DIR}/_deps/torch-src/share/cmake/Torch/")
find_package(Torch REQUIRED HINTS "${torch_SOURCE_DIR}")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

# message(FATAL_ERROR ${TORCH_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBRARIES}"
)
target_include_directories(${PROJECT_NAME} PRIVATE ${TORCH_INCLUDE_DIRS})

# find_package(Torch REQUIRED)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

# add_executable(example-app example-app.cpp)
# target_link_libraries(example-app "${TORCH_LIBRARIES}")
# set_property(TARGET example-app PROPERTY CXX_STANDARD 17)



# add_executable(example-app example-app.cpp)
# target_link_libraries(example-app "${TORCH_LIBRARIES}")
# set_property(TARGET example-app PROPERTY CXX_STANDARD 17)
44 changes: 36 additions & 8 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
{ pkgs ? import <nixpkgs> {} }:
{pkgs ? import <nixpkgs> {}}:
(pkgs.buildFHSUserEnv {
name = "OBCPP";
targetPkgs = pkgs: (with pkgs; [
python3
ccache
# ninja
# pipenv

pkgs.mkShell {
buildInputs = [
pkgs.gcc
pkgs.cmake
pkgs.libclang
];
}
# LSP
# python39Packages.python-lsp-server

# C deps
glib
glibc
stdenv
zlib

gcc
cmake
libclang
xz
bzip2

]);
runScript = "bash";
profile = ''
LD_LIBRARY_PATH=${pkgs.zlib}/lib:${pkgs.bzip2}/lib:$LD_LIBRARY_PATH
# set SOURCE_DATE_EPOCH so that we can use python wheels
SOURCE_DATE_EPOCH=$(date +%s)
PATH=$HOME/.local/bin:$PATH
# PYTHONPATH=$HOME/.local/lib/python3.9/site-packages:$PYTHONPATH
# export PIP_PREFIX=$(pwd)/_build/pip_packages
# export PYTHONPATH="$PIP_PREFIX/${pkgs.python3.sitePackages}:$PYTHONPATH"
# export PATH="$PIP_PREFIX/bin:$PATH"
# unset SOURCE_DATE_EPOCH
'';
}).env
33 changes: 28 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@

#include "core/states.hpp"

int main() {
std::cout << "hello" << std::endl;
#include <opencv2/opencv.hpp>
#include <torch/torch.h>
#include <iostream>

using namespace cv;

int main()
{
std::cout << "hellasdfasdfTarget torch_cpu not found.o" << std::endl;

PreparationState state;

state.tick();

torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;

Mat output = Mat::zeros(120, 350, CV_8UC3);

PreparationState state;
// write text on the matrix:
putText(output,
"Hello World :)",
Point(15, 70),
FONT_HERSHEY_PLAIN,
3,
Scalar(0, 255, 0),
4);

state.tick();
imwrite("Output.png", output);

return 0;
return 0;
}

0 comments on commit f166071

Please sign in to comment.