Repository: conan-io/conan-center-index

Issue:
[request] whereami/20220112
### Package Details
  * Package Name/Version: **whereami/20220112**
  * Source code: **https://github.com/gpakosz/whereami**


### Description Of The Library / Tool

A drop-in two files library to locate the current executable and the current module on the file system.


While fixing https://github.com/conan-io/conan-center-index/issues/8674, I realized having whereami would have been nice.
There is an existing https://github.com/bincrafters/conan-whereami


Discussion:

Resolving patch:
diff --git a/recipes/whereami/all/conanfile.py b/recipes/whereami/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/whereami/all/conanfile.py
@@ -0,0 +1,59 @@
+from conans import ConanFile, CMake, tools
+
+required_conan_version = ">=1.33.0"
+
+
+class WhereamiConan(ConanFile):
+    name = "whereami"
+    description = "Locate the current executable and the current module/library on the file system"
+    topics = ("whereami", "introspection", "getmodulefilename",
+              "dladdr", "executable-path", "getexecutablepath")
+    url = "https://github.com/conan-io/conan-center-index"
+    homepage = "https://github.com/gpakosz/whereami"
+    license = ("MIT", "WTFPL")
+    exports_sources = ["CMakeLists.txt"]
+    generators = "cmake"
+    settings = "os", "arch", "compiler", "build_type"
+    options = {"shared": [True, False], "fPIC": [True, False]}
+    default_options = {"shared": False, "fPIC": True}
+
+    _cmake = None
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    def config_options(self):
+        if self.settings.os == 'Windows':
+            del self.options.fPIC
+
+    def configure(self):
+        if self.options.shared:
+            del self.options.fPIC
+        del self.settings.compiler.libcxx
+        del self.settings.compiler.cppstd
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version],
+                  destination=self._source_subfolder, strip_root=True)
+
+    def _configure_cmake(self):
+        if self._cmake:
+            return self._cmake
+
+        self._cmake = CMake(self)
+        self._cmake.configure()
+        return self._cmake
+
+    def build(self):
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def package(self):
+        self.copy(pattern="LICENSE.*", dst="licenses",
+                  src=self._source_subfolder)
+        cmake = self._configure_cmake()
+        cmake.install()
+
+    def package_info(self):
+        self.cpp_info.libs = ["whereami"]


Test patch:
diff --git a/recipes/whereami/all/test_package/CMakeLists.txt b/recipes/whereami/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/whereami/all/test_package/CMakeLists.txt
@@ -0,0 +1,10 @@
+cmake_minimum_required(VERSION 3.1)
+project(test_package C)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup(TARGETS)
+
+find_package(whereami REQUIRED CONFIG)
+
+add_executable(${PROJECT_NAME} test_package.c)
+target_link_libraries(${PROJECT_NAME} whereami::whereami)
diff --git a/recipes/whereami/all/test_package/conanfile.py b/recipes/whereami/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/whereami/all/test_package/conanfile.py
@@ -0,0 +1,17 @@
+from conans import ConanFile, CMake, tools
+import os
+
+
+class TestPackageConan(ConanFile):
+    settings = "os", "compiler", "build_type", "arch"
+    generators = "cmake", "cmake_find_package_multi"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        if not tools.cross_building(self):
+            bin_path = os.path.join("bin", "test_package")
+            self.run(bin_path, run_environment=True)
diff --git a/recipes/whereami/all/test_package/test_package.c b/recipes/whereami/all/test_package/test_package.c
new file mode 100644
--- /dev/null
+++ b/recipes/whereami/all/test_package/test_package.c
@@ -0,0 +1,46 @@
+#include <whereami.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main() {
+
+  int length = wai_getExecutablePath(NULL, 0, NULL);
+
+  if (length > 0) {
+    char* path = (char*)malloc(length + 1);
+    if (!path) {
+      abort();
+    }
+    int dirname_length = 0;
+    wai_getExecutablePath(path, length, &dirname_length);
+    path[length] = '\0';
+
+    printf("Executable path: %s\n", path);
+    path[dirname_length] = '\0';
+    printf("  dirname: %s\n", path);
+    printf("  basename: %s\n", path + dirname_length + 1);
+
+    free(path);
+  }
+
+  printf("\n");
+
+  length = wai_getModulePath(NULL, 0, NULL);
+  if (length > 0) {
+    char* path = (char*)malloc(length + 1);
+    if (!path) {
+      abort();
+    }
+    int dirname_length = 0;
+    wai_getModulePath(path, length, &dirname_length);
+    path[length] = '\0';
+
+    printf("module path: %s\n", path);
+    path[dirname_length] = '\0';
+    printf("  dirname: %s\n", path);
+    printf("  basename: %s\n", path + dirname_length + 1);
+    free(path);
+  }
+
+  return 0;
+}