Skip to content

Instantly share code, notes, and snippets.

@xahon
Last active April 6, 2023 21:15
Show Gist options
  • Save xahon/77f62cbbe2c2afa28d91021aad71d5d2 to your computer and use it in GitHub Desktop.
Save xahon/77f62cbbe2c2afa28d91021aad71d5d2 to your computer and use it in GitHub Desktop.
Creates a conan helper to easily inject C/C++ dependencies in CMake-based project
# Usage: cd <project_root> && python ./ezdeps.py
# Add "include(ezdeps/CMakeLists.txt)" at the top of the root CMakeLists.txt
# Add needed dependencies in ezdeps/conanfile.py
import os
import urllib.request
from builtins import print
cwd = os.getcwd()
if not os.path.exists(os.path.join(cwd, 'ezdeps')):
os.mkdir(os.path.join(cwd, 'ezdeps'))
os.chdir(os.path.join(cwd, 'ezdeps'))
with open('CMakeLists.txt', 'w') as f:
conanfile_path = os.path.join(cwd, "ezdeps/conanfile.py").replace("\\", "/")
f.write('include(${CMAKE_SOURCE_DIR}/ezdeps/conan.cmake)\n\n')
f.write('conan_cmake_autodetect(settings)\n')
f.write('conan_cmake_run(\n')
f.write(' BASIC_SETUP\n')
f.write(' KEEP_RPATHS\n')
f.write(' CMAKE_TARGETS\n')
f.write(' BUILD missing\n')
f.write(f' CONANFILE {conanfile_path}\n')
f.write(' SETTINGS ${settings}\n')
f.write(')\n')
f.write('include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)\n')
with open('conanfile.py', 'w') as f:
f.write('from conans import ConanFile\n\n')
f.write('class Project(ConanFile):\n')
f.write(' settings = "os", "compiler", "build_type", "arch"\n')
f.write(' generators = "cmake_find_package_multi"\n\n')
f.write(' # def requirements(self):\n')
f.write(' # self.requires("zlib/1.2.13")\n')
f.write(' \n')
f.write(' # def configure(self):\n')
f.write(' # self.options["sdl"].sdl2main = True\n')
url = 'https://raw.githubusercontent.com/conan-io/cmake-conan/develop/conan.cmake'
urllib.request.urlretrieve(url, os.path.join(cwd, 'ezdeps/conan.cmake'))
print('Add next line to the root CMakeLists.txt:\ninclude(ezdeps/CMakeLists.txt)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment