Skip to content

Commit

Permalink
Add function to detect nvcc version.
Browse files Browse the repository at this point in the history
  • Loading branch information
theComputeKid committed May 25, 2024
1 parent 6a0958b commit 7a44cd2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Compiles CUDA code using the Visual Studio CUDA Toolkit extension on Windows and
- `cudaGenLineInfo` (Bool) -> generates line info.
- `cudaIntDir` (String) -> Intermediary directory for CUDA files - Windows only.

The following functions are provided:
- `detectNvccVersion()` -> try to detect the default version of nvcc on the system.
- `detectNvccVersion(cudaPath)` -> try to detect the version of nvcc from a provided path.

----------------
Notes for Windows:
----------------
Expand Down
2 changes: 2 additions & 0 deletions premake5-cuda.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ if os.target() == "windows" then
elseif os.target() == "linux" then
dofile("src/premake5-cuda-nvcc.lua")
end

dofile("src/utils.lua")
24 changes: 24 additions & 0 deletions src/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- * Returns the version of nvcc detected on the system, or 0 if undetected.
function detectNvccVersion(cudaPath)
local errorCode = 1
local output

if cudaPath ~= nil then
output, errorCode = os.outputof("/usr/local/cuda/bin/nvcc --version")
else
if os.host() == "linux" then
output, errorCode = os.outputof("/usr/local/cuda/bin/nvcc --version")
elseif os.host() == "windows" then
output, errorCode = os.outputof("nvcc --version")
else
return 0
end
end

if not errorCode == 0 then
return 0
end

local versionString = output:match("cuda_(%S+)")
return versionString:match("[^.]*.[^.]*")
end
8 changes: 5 additions & 3 deletions test/premake5.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require("../premake5-cuda")

local cudaVer = detectNvccVersion()

workspace "ExampleProject"

language "C++"
Expand Down Expand Up @@ -48,7 +50,7 @@ project "ExampleProjectExe"

files { "exe/cpp/**.cpp" }

buildcustomizations "BuildCustomizations/CUDA 12.1"
buildcustomizations("BuildCustomizations/CUDA " .. cudaVer)

externalwarnings "Off" -- thrust gives a lot of warnings

Expand Down Expand Up @@ -82,7 +84,7 @@ project "ExampleProjectDLL"

location "out/%{prj.name}"

buildcustomizations "BuildCustomizations/CUDA 12.1"
buildcustomizations("BuildCustomizations/CUDA " .. cudaVer)

if os.target() == "windows" then
-- Just in case we want the VS CUDA extension to use a custom version of CUDA
Expand All @@ -100,7 +102,7 @@ project "ExampleProjectDLL"
defines { "PREMAKE_CUDA_EXPORT_API" }

-- Let's compile for all supported architectures (and also in parallel with -t0)
cudaCompilerOptions {"-arch=all", "-t0"}
cudaCompilerOptions {"-arch=all", "-t0"}

filter "configurations:debug"
cudaLinkerOptions { "-g" }
Expand Down
2 changes: 1 addition & 1 deletion vendor/premake-core
Submodule premake-core updated 193 files

0 comments on commit 7a44cd2

Please sign in to comment.